C #: Performing conditional with object initializer syntax

Suppose I have a simple class that adds:

public class Multiply
{
     public int A {get; set;}
     public int B {get; set;}
     public int C {get; set;}

     public List<int> Result {get; set;}

     public void Calculate()
     {
         if (A != 0 && B!= 0 && C != 0)
         {
           Result.Add(A);
           Result.Add(B);
           Result.Add(C);
           Result.Add(A * B);
           Result.Add(A * C);
           Result.Add(B * C);
           Result.Add(A * B * C);
         }
     }
}

The above class models my actual application. I have a number of parameters that are set, in this case A, B and C. Then I execute Calculate and use the Result property of the Multiply object to access the result.

(There may be more efficient ways to execute this template, with lazy loading coming to mind. If you want to suggest a better template for this, but this is not the purpose of my question, but just a simple example illustrating my question.)

Here is my question:

If I use Object Intializer syntax:

Multiply m = new Multiplier()
{
   A = 1,
   B = 2,
   C = 3
}

m.Calculate();
DoSomething(m.Result[5]); //DoSomething(6); 

Is there a way to perform Calculate()as part of initialization m?

+3
source share
2

Calculate.

, . , #.

+5

, ...

: getter Result.

Edit:

- , .

, . , A, B C, . , , , .

+1

Source: https://habr.com/ru/post/1794049/


All Articles