1

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!!

2
  • Wild assume, you probably get an NullReferenceException on field friends? Commented Oct 19, 2022 at 12:51
  • You've not initialised List<Friend> friends anywhere I can see. Either do it in the constructor for Student, or do it somewhere else once you've created the Student object Commented Oct 19, 2022 at 12:59

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your quick response and clear-cut explanation!!!,
Surely, will follow the conventions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.