3

Is there a way to instanciate an object automatically without instanciate it in the constructor ?

Actually, I have something like :

public List<Object> Products { get; set; }
public MyClass()
{
    this.Products = new List<Object>();
}

But instead, I would like to instanciate directly my list without specifying in my constructor of my class with something like :

public List<Object> Products = new List<Object>(); { get; set; }

Is there any trick to do this?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Charles SB
  • 128
  • 2
  • 8

3 Answers3

4

I think you're asking whether you can give an initial value to an automatically-implemented property. The answer to that question is "no".

You could write a manual property though:

private List<Product> products = new List<Product>();

public List<Product> Products
{
    get { return products; }
    set { this.products = value; }
}

Do you actually need the setter at all? Do you need clients to be able to replace the variable value with another list? You may find that you only need a read-only property, in which case you could write:

private readonly List<Product> products = new List<Product>();

public IList<Product> Products { get { return products; } }

Note that here you can still use a collection initializer, e.g.

Foo foo = new Foo
{
    Products = { new Product("foo"), new Product("bar") }
};
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

As Jon Skeet Says :

It's unfortunate that there's no way of doing this right now. You have to set the value in the constructor. (Using constructor chaining can help to avoid duplication.)

Automatically implemented properties are handy right now, but could certainly be nicer. I don't find myself wanting this sort of initialization as often as a read-only automatically implemented property which could only be set in the constructor and would be backed by a read-only field. It's possible that both of these will be fixed in C# 5, which I strongly hope will address immutability concerns. (I don't think either of them are scheduled for C# 4.)

Source : Initializing C# auto-properties


If you need to initialize the property without using the constructor, you need to use a backing field.

Example

class Demo
{
    private List<object> myProperty = new List<object>();
    public List<object> MyProperty
    {
        get { return myProperty; }
        set { myProperty = value; }
    }
}
Community
  • 1
  • 1
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
1

In C#, no not currently with an auto-generated property (though VB.NET auto-properties can do this). This is something I have often wished for myself. So you could manually roll your own property, or use a constructor.

Either way you'll have some code you wish you didn't have to write to do this (manual property with no constructor vs. auto-property with constructor).

Interestingly, though, the IL is different between the two options. When you have a manual property with a backing field using an initializer, it will get executed before the base-class constructor in C#:

MyClass..ctor:
IL_0000:  ldarg.0     
IL_0001:  newobj      System.Collections.Generic.List<System.Object>..ctor
IL_0006:  stfld       UserQuery+MyClass._products
IL_000B:  ldarg.0     
IL_000C:  call        System.Object..ctor
IL_0011:  nop         
IL_0012:  ret         

While using an auto-property with initialization in the constructor will get executed after the base-class constructor in C#:

MyClass..ctor:
IL_0000:  ldarg.0     
IL_0001:  call        System.Object..ctor
IL_0006:  nop         
IL_0007:  nop         
IL_0008:  ldarg.0     
IL_0009:  newobj      System.Collections.Generic.List<System.Object>..ctor
IL_000E:  call        UserQuery+MyClass.set_Products
IL_0013:  nop         
IL_0014:  nop         
IL_0015:  ret       

I'm only saying this as a point of curiosity, as writing code that depends on initialization order in .NET can be risky (initialization order in fact differs a bit between VB.NET and C#)

James Michael Hare
  • 37,767
  • 9
  • 73
  • 83
  • +1, becuase you know to read IL! I prefer to learn Chinese or any other foreign lang before IL... – gdoron Feb 12 '13 at 19:55