-1

I known my question look like personal satisfaction. But I don't understand What purpose if bracket style not difference from normal way.

class A
{
  public Boolean Property1 { get; set; }
  public Boolean Property2 { get; set; }
  public Boolean Property3 { get; set; }
  public Boolean Property4 { get; set; }
}

static void TestClassPropertyInitialize()
{
  A a = new A() // Not complete property. Should error ?
  {
    Property1 = true,
    Property2 = false,
  };

  // Normal style
  a.Property3 = false;
  a.Property4 = false;
}

From @steve recomment this topic. What's the difference between an object initializer and a constructor?

But my question is focus on properties setting (Not constructure). I known this question difficult to answer because it 's depend on designer language.

More info. Should error ? in my sample code. Meaning why this line of code not cause complie-time error. Because only 2 of 4 properties setting.

Pranay Rana
  • 170,430
  • 35
  • 234
  • 261
  • Possible duplicate of [What's the difference between an object initializer and a constructor?](https://stackoverflow.com/questions/740658/whats-the-difference-between-an-object-initializer-and-a-constructor) – Steve Apr 24 '18 at 04:22
  • It's just another way (type a bit less?) to initialize a type. And no it should not throw an error. – gsharp Apr 24 '18 at 04:22
  • @Steve it's not a duplicate. – gsharp Apr 24 '18 at 04:23
  • @gsharp I don't see why not. OP appears to be asking the difference between the styles. The duplicate covers this and more. – Steve Apr 24 '18 at 04:25
  • I'm not sure what are you asking here. Why do you think there should be an error? – Zohar Peled Apr 24 '18 at 04:28
  • @Steve It's more than "what is the difference?" This question is asking "why does this difference exist?" These questions are always kind of vague and difficult to answer. "Because the language designers said so" isn't very satisfying but is kind of what it comes down to. Of course one could go into an extended discussion. In this case object initializers were added in C# 3, probably to harmonize "named types" with the desired syntax for anonymous types. – Mike Zboray Apr 24 '18 at 04:29
  • Steve. I follow your topic. But It 's difference my topic focus on "properties" set not constructor. – Sum Settavut Apr 24 '18 at 04:30
  • 1
    The question is unclear. Are you asking, 'if I only set 2 properties, not all 4, why is this not an error?'? In which case, Steve's link should help, I think. – wazz Apr 24 '18 at 04:37
  • Generally setting properties would always be considered optional so what is the justification for making it an error? Is it just because you think there should be a purpose for object initializers beyond another way to do the same thing? Note that making it an error raises backwards compatibility issues. If I use a library in version 1 with only Property1 and Property2 and my code compiles, then when version 2 adds Property 3, my code now fails to compile. Many people would find this very frustrating. – Mike Zboray Apr 24 '18 at 04:42
  • Or perhaps you are asking why you don't need the `a.` when initializing the properties inside `{...}`? – Zohar Peled Apr 24 '18 at 04:44
  • @mike z. OK Your answer still not clear. Because this style add later. Then prior version is not using Right ? How conflict ? – Sum Settavut Apr 24 '18 at 04:47
  • @user282341 Do you think it should be an error to not set a property in an object initializer? What did you mean by "Should error ?" ? – Mike Zboray Apr 24 '18 at 04:49
  • https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-objects-by-using-an-object-initializer *You can use object initializers to initialize type objects in a declarative manner without explicitly invoking a constructor for the type* that's it, that's all there is too it, there is no mystery no collusion, and no more reasons why – TheGeneral Apr 24 '18 at 04:50
  • @mikez I think should error at least complie-time. Because property not complete set. I though this style should act like array initialized error if index mismatch. This is my personal opinion. – Sum Settavut Apr 24 '18 at 04:52
  • @user282341 maybe you should write to BillGates@the.programmer.is.always.right.at.Microsoft.com . however seriously, the feature does exactly what it says it does, no more no less – TheGeneral Apr 24 '18 at 04:56
  • @TheGeneral. OK Then forget it. :P – Sum Settavut Apr 24 '18 at 05:02
  • @user282341 Ok You seem to be mixing two different things. Ignore the comment about C# 3 for the moment. I am saying that your proposed behavior (causing an error) would cause version incompatibilities for users which would be annoying to resolve. The proposed error just does not seem at all useful in general, since properties are considered optional to set. – Mike Zboray Apr 24 '18 at 05:04
  • Is it possible that you are thinking of structs and the warnings/errors you get when not initializing all members? – Emond Apr 24 '18 at 05:04

2 Answers2

1

In C# term this is object initialization (in technical term its Syntactic sugar) , as based on that you are creating object A and initializing proper1 and property2

  A a = new A() // Not complete property. Should error ?
  {
    Property1 = true,
    Property2 = false,
  };
  //this one is added so you dont have to do like this 
  //A a = new A();
  //a.property1 = true;
  //a.property2 = true;
  //a.property3 = true;
  //a.property4 = true;

you will not get any error as the other two properties are not mandatory.

IF you want to make it mandatory then you should have constructor like

class A
{
  public A(bool prop1,bool prop2,bool prop3,bool prop4) {
     this.Property1 = prop1;
     //same for other property
  }
  public Boolean Property1 { get; set; }
  public Boolean Property2 { get; set; }
  public Boolean Property3 { get; set; }
  public Boolean Property4 { get; set; }
}

now to initialize it you need to code like this

 A a = new A(true,false);//this given error as there are four input value
 A a = new A(true,false,true,false);//this will work 
Pranay Rana
  • 170,430
  • 35
  • 234
  • 261
0
A a = new A
  {
    Property1 = true,
    Property2 = false,
  };

Is functionally identical as:

A a = new A();
a.Property1 = true;
a.Property2 = false;

I believe it's syntactic sugar. However you can do this and still look fairly pretty:

List<StudentName> students = new List<StudentName>()
{
  new StudentName {FirstName="Craig", LastName="Playstead", ID=116},
  new StudentName {FirstName="Shu", LastName="Ito", ID=112},
  new StudentName {FirstName="Gretchen", LastName="Rivas", ID=113},
  new StudentName {FirstName="Rajesh", LastName="Rotti", ID=114}
};

And in a lot less lines of code.

Edit: No it will not warn you that you didn't set the other properties.