Call_me_raj
Call_me_raj

Reputation: 19

How to add data multiple nested lists of Objects in C#

In this example, i am able to create new student object and able to add data to its 2 variables called Id and Name but i am facing difficulty and not able to add third variable (List -> i.e. friends)

using System;
using System. LINQ;
using System. Collections. Generic;

public class Program
{
   //public List<Student> students;
    public static void Main()
    {
        var students = new List<Student>();
        students. Add(new Student
        {
           Id = 120228,
           Name = "Rajesh",
        });
        students. Add(new Student
        {
           Id = 120229,
           Name = "Mahesh",
        });
    
        foreach(var student in students)
            Console. WriteLine(student.Id + ", " +student.Name);
    }
}

public class Student
{ 
    public int Id;
    public string Name;
    public List<Friend> friends;
}

public class Friend {
    public int friendId;
    public string friendName;
}

How should i add data to "friends"(List<Friend>) variable like other first 2 fields. (if possible help me with working code)

//finally, i want to print all details of the student.

foreach(var student in students){
      foreach(var friend in student.friends){
          Console. WriteLine(student.Name+ " friend name is " +friend.Name);
      }
}

Thank you for your valuable time!!

Upvotes: 1

Views: 1287

Answers (1)

Jeroen van Langen
Jeroen van Langen

Reputation: 22083

Before you can add friends, you need to instantiate the friendlist first. This can be done inline like this:

students.Add(new Student
{
   Id = 120229,
   Name = "Mahesh",
   friends = new List<Friend>
   {
        new Friend
        {
            friendId = 10,
            friendName = "John"
        },
        new Friend
        {
            friendId = 11,
            friendName = "Jane"
        },
   }
});

But if you want to add friends after construction of a new Student. You need to instantiate the list before adding friends:

var student = new Student
{
    Id = 120229,
    Name = "Mahesh",
};

// instantiate the friendslist before adding an item.
student.friends = new List<Friend>();

student.friends.Add(new Friend
{
    friendId = 10,
    friendName = "John"
});

Normally I would choose to construct the friendlist inside the student, because you probably add new friends later. The problem is, otherwise you need to check if the friends field is not assigned(not instantiate) and you have to create it:

public class Student
{ 
    public int Id;
    public string Name;
    public List<Friend> friends = new List<Friend>();
}


var student = new Student
{
    Id = 120229,
    Name = "Mahesh",
};

// instantiate the friendslist before adding an item.
student.friends.Add(new Friend
{
    friendId = 10,
    friendName = "John"
});

A tip: Note the code writing conventions. The Student Id/Name uses PascalCase, but the friends and Friend class fields are using camelCase. Try use the same casing of existing code, otherwise it will become a mess.

Upvotes: 3

Related Questions