rajeshnrh
rajeshnrh

Reputation: 55

C# Initialize Class with a List<T> property

I need some help on how to initialize the below object with some sample values in the Main method to perform some action.

Since I am new to C# please guide me to where can i get this information

class MobOwner
{
   public string Name { get; set; }
   public List<string> Mobiles { get; set; }
}

Upvotes: 3

Views: 22044

Answers (6)

Reza Shafie
Reza Shafie

Reputation: 101

If I'm getting your purpose correctly you want to initialize these values in the "Main" method.

Constructor is a good way to initialize your properties with default values whenever you create an instance of your class. But if you want to initialize them in another place make an instance of your class and then you can give values to its public members. like this:

MobOwner mobOwner = new MobOwner();
mobOwner.Name = "Jimmy";
mobOwner.Mobiles = new List<string>{119, 011};

or in a more modern way you can change the syntax like this(although they are the same):

MobOwner mobOwner = new(){
    Name = "Jimmy",
    Mobiles = new List<string>{119, 011}
};

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

First of all, I doubt if you really want set; in the Mobiles property: typically we add/update/remove items in the list, but not assign the list as whole

  MobOwner sample = new MobOwner(...);

  sample.MobOwner.Add("123");
  sample.MobOwner.Add("456");
  sample.MobOwner.RemoveAt(1);
  sample.MobOwner[0] = "789";

  sample.MobOwner = null; // we, usually, don't want such code

The implementation can be

 class MobOwner {
   public string Name { get; set; } 
   public List<string> Mobiles { get; } = new List<string>();

   public MobOwner(string name, IEnumerable<string> mobiles): base() {
     if (null == name)
       throw new ArgumentNullException("name");

     if (null == mobiles)
       throw new ArgumentNullException("mobiles");

     Name = name;

     Mobiles.AddRange(mobiles); 
   }
 }

Upvotes: 4

fubo
fubo

Reputation: 45947

This creates one MobOwner object containing a list with one item

MobOwner item = new MobOwner()
{
    Name = "foo",
    Mobiles = new List<string>() { "bar" }
};

Another way is to add a constructor to simplify instanciation

class MobOwner
{
    public string Name { get; set; }
    public List<string> Mobiles { get; set; }

    public MobOwner(string Name, params string[] Mobiles) 
    {
        this.Name = Name;
        this.Mobiles = new List<string>(Mobiles);
    }
}

usage:

MobOwner item2 = new MobOwner("foo", "bar", "bar");

Upvotes: 0

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

Simply initialize it within your constrcutor:

class MobOwner
{
    public string Name { get; set; }
    public List<string> Mobiles { get; set; }
    public MobOwner() {
        this.Mobiles = new List<string>();
    }
}

You can also define a constructor that direclty puts the right values into your list:

class MobOwner
{
    public string Name { get; set; }
    public List<string> Mobiles { get; set; }
    public MobOwner(IEnumerable<string> values) {
        this.Mobiles = values.ToList();
    }
}

Which you can than call like new MobOwner(new[] { "Mario", "Hans", "Bernd" })

Upvotes: 7

Dmitriy Kovalenko
Dmitriy Kovalenko

Reputation: 3616

var mobOwner = new MobOwner()
   {
       Name = "name";
       Mobiles = new List<string>()
           {
               "mob1",
               "mob2",
               "mob3"
           };
    };

Upvotes: 0

lordkain
lordkain

Reputation: 3109

you can make and instance and set the variable

var owner = new MobOwner();
owner.Mobiles = new List<string>{"first", "second"};

or like so

var owner = new MobOwner {Mobiles = new List<string> {"first", "second"}};

recommanded way is to use a contructor and make the set properties private

class MobOwner
{
    public string Name { get; private set; }
    public List<string> Mobiles { get; private set; }
    // constructor
    public MobOwner(string name, List<string> mobiles)
    {
        Name = name;
        Mobiles = mobiles;
    }
}

Upvotes: 1

Related Questions