Results 1 to 4 of 4

Thread: [C# 3.0+] Initialiser Syntax

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    [C# 3.0+] Initialiser Syntax

    VB version here.

    C# 3.0 introduced initialiser syntax, which allows you to initialise the property values of a newly created object in a block that is basically attached to the constructor, e.g.
    CSharp Code:
    1. SomeType myObject = new SomeType { Property1 = value1, Property2 = value2 };
    You might ask what's the use of this. Why is it any better than doing this:
    CSharp Code:
    1. SomeType myObject = new SomeType();
    2.  
    3. myObject.Property1 = value1;
    4. myObject.Property2 = value2;
    Well, it's a bit more succinct than that for a start, but that's not where the real advantage lies. The property (or field) initialisation is actually part of the expression that returns a reference to the newly created object. As such you can use initialiser syntax in places where you couldn't use a conventional pattern because it would require multiple lines of code. One example is when initialising member variables, e.g.
    CSharp Code:
    1. private SomeType myObject = new SomeType { Property1 = value1, Property2 = value2 };
    Normally you'd have to set the properties in the form's Load event handler or something like that. Just note that the values must be literals or static members. Instance members are not available outside a method or property in C# as they are in VB.

    Another place where this is useful is when you want to create one object and then pass it to the constructor, or some method, of a second, but you want to set one or more properties of the first object. Previously you'd have had to do something like this:
    CSharp Code:
    1. SomeType obj1 = new SomeType();
    2.  
    3. obj1.SomeProperty = someValue;
    4.  
    5. SomeOtherType obj2 = new SomeOtherType(obj1);
    Now you can do this:
    CSharp Code:
    1. SomeOtherType obj2 = new SomeOtherType(new SomeType { SomeProperty = someValue });
    This is not a revolutionary change, but it does let you keep your code more concise in certain circumstances.

    One thing I would caution against is using initialiser syntax in situations where it actually makes your code harder to read, which may happen if you nest too deeply or have lines that are too long. To help avoid this in certain circumstances, I strongly recommend using line continuation to make your code more readable, e.g.
    CSharp Code:
    1. SomeType myObject = new SomeType { Property1 = value1,
    2.                                    Property2 = value2 };

  2. #2
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [C# 3.0+] Initialiser Syntax

    cool, didn't know that. Although if I ever need to do that I usually toss the values into the constructor.

  3. #3

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: [C# 3.0+] Initialiser Syntax

    Quote Originally Posted by high6
    cool, didn't know that. Although if I ever need to do that I usually toss the values into the constructor.
    It's most useful with classes you get from elsewhere that you have no control over. It's also handy for structures that you want to keep as minimal as possible by declaring member fields only with no other members.

  4. #4
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Re: [C# 3.0+] Initialiser Syntax

    Quote Originally Posted by jmcilhinney
    It's most useful with classes you get from elsewhere that you have no control over. It's also handy for structures that you want to keep as minimal as possible by declaring members fields only with no other members.
    At my company we actually use that method for assigning values to all our DataContracts. As long as it's well documented which properties are assigned to and which aren't when sent over the wire, it allows for very fast creation of entities.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width