-1

I've the following class

MyClass
{

    public int Id {get;set;}
    public string Name {get;set;}
    public List<string> FriendNames {get;set;} 

    public MyClass()
    {
       FriendNames = new List<string>();
    }
}

Is it correct to initialise the List like I've done or should it be

this.FriendNames = new List<string>;

Is there any difference ?

Then in my code I can create a instance like

MyClass oMyClass = new MyClass();
oMyClass.Id = 1;
oMyClass.Name = "Bob Smith";
oMyClass.FriendNames.Add("Joe King");
neildt
  • 5,101
  • 10
  • 56
  • 107
  • 1
    Usually when you have a collection in a class you will mark it `{get; private set;}` so external users can not replace the underlying collection. – Scott Chamberlain Nov 13 '13 at 17:30
  • possible duplicate of [When do you use the "this" keyword?](http://stackoverflow.com/questions/23250/when-do-you-use-the-this-keyword) – nawfal Nov 13 '13 at 17:55

7 Answers7

0

There is no real difference. So it boils down to preference.

Some people like to because it is very explicit and self commenting. Others feel that it clutters the code.

The important thing is that you are instantiating the List in the constructor. Something that too many people forget to do.

The accepted answer in this SO question has a nice list of when to use the this keyword

When do you use the “this” keyword?

Community
  • 1
  • 1
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
0

It should be

FriendNames = new List<string>();

Or

FriendNames = new List<string>(123456);

if you know the capacity before upon initialization.

Stachu
  • 5,677
  • 3
  • 30
  • 34
Recursor
  • 542
  • 5
  • 18
0

You could do this.

    private List<string> list = new List<string>();

    public List<string> List
    {
        get { return list; }
        set { list = value; }
    }
DROP TABLE users
  • 1,955
  • 14
  • 26
0

Instantiating a list should be something like

FriendNames = new List<string>();

If you're asking whether the this is required, if your constructor took a list of FriendNames such that there is ambiguity in what you mean, then you'd have to use this to indicate that you want to assign it to the object's instance.

public MyClass(List<String> FriendNames)
{
   this.FriendNames = FriendNames;
}
MxLDevs
  • 19,048
  • 36
  • 123
  • 194
0

If you are using this.FriendNames = new List<string>; you see that this is a class-variable with the first glance.

Especially in large classes with many variables this can be an advantage as you can easier/faster distinguish class-variables from local variables.

But as many other here stated this is up to your preference.

Where this is getting important is if you have a class and a local variable with the same name like this:

MyClass
{
    public List<string> FriendNames {get;set;} 

    public SetMyList(List<string> FriendNames)
    {
       this.FriendNames = FriendNames;
    }
}

In this case you have to use the this-keyword.

user1567896
  • 2,398
  • 2
  • 26
  • 43
0

Here this.FriendNames = new List<string>; is exactly the same as FriendNames = new List<string>; this will count only when you will have variable with name same as your property.

like here:

public MyClass(List<string> FriendNames)
{
    this.FriendNames = FriendNames; // here we have to specify which FriendNames is from this class
}

Constructor is for initializing some resources, so initializing your list there is a good idea.

You can create constructor with name and id (if you are always assigning them)

public MyClass(string name, int id)
{
   FriendNames = new List<string>();
   Name = name;
   Id = id;
}

and change your code to:

MyClass oMyClass = new MyClass("Bob Smith", 1);
oMyClass.FriendNames.Add("Joe King");
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
0

Use this to initialise an instance of your class

var x = new MyClass { Id = 1, Name = "Steve", 
            FriendNames = { "John", "Paul", "Ringo", "George" } };

And generally don't use "this." references, I rarely see it used in this way (often I use the only remnants of any kind of Hungarian notation left in my code by prefixing private fields with "_" so at least

public void DoSomething();
{
   _myInt = 123;
}
private int _myInt;
public int MyInt { get { return _myInt; } }

Means I know whats going on, but I'm sure people out there won't like the "_".

Steve Adams
  • 561
  • 4
  • 8
  • using `new MyClass { Id = 1, Name = "Steve", ` should be replaced with constructor – Kamil Budziewski Nov 13 '13 at 17:45
  • You're offering an alternative - explicity writing a constructor to take a set of parameters. But this is not "the right way" to do it. When microsoft introduced this style of initialiser list it meant I didn't have to write multiple overloaded constructors anymore. Most of the time you want to be able to flexibly create and initialise an object, constructors don't let you do this. – Steve Adams Nov 14 '13 at 11:14