8 Replies - 2134 Views - Last Post: 07 March 2011 - 12:44 PM Rate Topic: -----

#1 batesy3k   User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

List<T> Property shortcut

Posted 07 March 2011 - 02:53 AM

Refering to thread http://www.dreaminco...do-you-use-var/, it was mentioning property shortcuts using get;set; for convience, however, I found that this cannot be done using List<T>...
class Test
{
    public Test()
    {
    }

    public List<string> Users
    {
        get;
        set;
    }
}


Form1:
public Form1()
{
    InitializeComponent();

    Test t = new Test();
    t.Users.Add("Bob"); // << ERROR: Object reference not set to an instance of an object.
}


Is this a bug, or does List<T> explicity require the use of defined fields within a class before it can use used?

Unless I have got the wrong end of the stick, could someone explain to me :)

Is This A Good Question/Topic? 0
  • +

Replies To: List<T> Property shortcut

#2 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: List<T> Property shortcut

Posted 07 March 2011 - 03:07 AM

Hi,

It's not a bug :)

Basically, when you use an auto implemented property, the compiler automatically creates a backing field to go behind the property. The compiler initialises the field to it's default value. The default value of a field of type List<string> is null. Therefore, on this line,

t.Users.Add("Bob");



the backing field behind the property is null, because the compiler doesn't automatically create an instance of List<T>, hence the error. Therefore, you have to explicitly create an instance (just as you would if you were working with a field directly), like this:

//initialise a new list
Users = new List<string>();
//now you can add to it
Users.Add("Bob");


This post has been edited by [email protected]: 07 March 2011 - 03:13 AM

Was This Post Helpful? 2
  • +
  • -

#3 batesy3k   User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: List<T> Property shortcut

Posted 07 March 2011 - 03:13 AM

OK thanks :)

So is this the correct way of doing it?
public Form1()
{
    InitializeComponent();

    Test t = new Test();
    t.Users = new List<string>();
    t.Users.Add("Bob");
}


Seems a bit odd that it doesn't create a backing field imo :)
Was This Post Helpful? 0
  • +
  • -

#4 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: List<T> Property shortcut

Posted 07 March 2011 - 03:21 AM

Yes, that will work :)

View Postbatesy3k, on 07 March 2011 - 10:13 AM, said:

Seems a bit odd that it doesn't create a backing field imo :)


It does create a backing field, but it just initialises it to its default value. This makes complete sense if you think about it. If you declare a field like this:

//this is something like what the backing field could look like actually
private List<string> users;



'users' is null because you havn't created an instance of the list yet. You want to be able to control when you create instances of objects. You don't want instances to be made automatically for you.

In the same way, if you had this property (that operates on type int):

public int Number{get;set;}


, the field behind it that is automatically created could look something like this:

private int number;


'number' will be given it's default value, just as it would if it were a stand alone field (in this case, default is 0). You wouldn't want 'numbers' to be assigned just some random number would you. You want it to be initialised to the default value until you decide what to put in there :)

This post has been edited by [email protected]: 07 March 2011 - 03:45 AM

Was This Post Helpful? 2
  • +
  • -

#5 batesy3k   User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: List<T> Property shortcut

Posted 07 March 2011 - 03:34 AM

That makes good sense! Cheers for clearing it up for me :) Had wondered for a while about this, and it was the other thread that jogged my memory :)
Was This Post Helpful? 0
  • +
  • -

#6 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: List<T> Property shortcut

Posted 07 March 2011 - 06:48 AM

To make the code "cleaner" for the main class, initialize the property in the constructor.
class Test
{
    public Test()
    {
        // Initialize properties to values you think are better defaults
        Users = new List<string>();
    }

    public List<string> Users
    {
        get;
        set;
    }
}

public Form1()
{
    InitializeComponent();

    Test t = new Test();
    // this line is no longer needed. Test takes care of this for you.
    //t.Users = new List<string>();
    t.Users.Add("Bob");
}

Was This Post Helpful? 1
  • +
  • -

#7 batesy3k   User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: List<T> Property shortcut

Posted 07 March 2011 - 08:36 AM

That is very true! However, what if the property was static in a static class because you don't have constructors? Would the way I wrote it then be sufficient?

This post has been edited by batesy3k: 07 March 2011 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

#8 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: List<T> Property shortcut

Posted 07 March 2011 - 08:50 AM

Yes, but you would obviously access it through the class name rather than an instance:

Test.Users = new List<string>();


To be honest though, I would create a field myself, and instantiate it in the class and then expose it using a normal property.


//if I wanted a list to allow the users to add and remove items from, I could do this:
 
private static IList<string> users = new List<string>();

public static IList<string> Users{ get{return users;} }



I wouldn't usually use automatic properties for exposing collections (partly because they demand a set accessor is provided). It should usually be the class containing the list responsibility to initialize the list as and when required, so I haven't provided a set accessor in the above example (but you could if you really wanted/needed to).

You could of course just implement an automatic property and make the set accessor private... Although, creating my own backing field just makes more sense to me as far as collections are concerned...

I guess it's ultimately style thing. Plus, it depends on what your trying to achieve.

I would also tend to question whether or not the class should be static if you are exposing a list publicly for modification, as it sort of implies the class is going to rely (at least partly) on object state :)

This post has been edited by [email protected]: 07 March 2011 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: List<T> Property shortcut

Posted 07 March 2011 - 12:44 PM

Static classes can have constructors. Make a default constructor and mark it static, and the code there will be executed when the static fields of the class are created.

This constructor can only access static resources.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1