Initiate properties at the creation of the object.
-
I know that the code snippet below is equal, but which is better?
Sample sm = new Sample()
{
prop1 = 1,
prop2 = 2
};and
Sample sm = new Sample();
sm.prop1 = 1;
sm.prop2 = 2;Is there any advantage and disadvantage on both of them? what's your thought about this? thanks
-
I know that the code snippet below is equal, but which is better?
Sample sm = new Sample()
{
prop1 = 1,
prop2 = 2
};and
Sample sm = new Sample();
sm.prop1 = 1;
sm.prop2 = 2;Is there any advantage and disadvantage on both of them? what's your thought about this? thanks
In the example you show, there is no difference between the two. The object initializer approach was introduced initially to cope with creating anonymous objects - it was the only way to define the "shape" of the object if it was anonymous.
This space for rent