30

This must be really simple but just not getting my syntax right here. let's say we have classes like two below:

class Student
{
    Subject[] subjects;
}

class Subject
{
    string Name;
    string referenceBook;
}

Here is my code:

Student univStudent = new Student();

Now, I want to add subjects here but not able to do something like

univStudent.subjects.add(new Subject{....});

How do i add items to this object array?

8 Answers 8

71

You can try

Subject[] subjects = new Subject[2];
subjects[0] = new Subject{....};
subjects[1] = new Subject{....};

alternatively you can use List

List<Subject> subjects = new List<Subject>();
subjects.Add(new Subject{....});
subjects.Add(new Subject{....});
// Then you can convert the List to Array like below:
Subject[] arraySubjects = subjects.ToArray<Subject>() 
Sign up to request clarification or add additional context in comments.

2 Comments

I used the 2nd way and did a .ToArray<Subject>()
subjects.add may need to be subjects.Add
19

You can use class System.Array for add new element:

Array.Resize(ref objArray, objArray.Length + 1);
objArray[objArray.Length - 1] = new Someobject();

Comments

8

You can't. However, you can replace the array with a new one which contains the extra element.

But it is easier and gives better performance to use an List<T> (uses interface IList) for this. List<T> does not resize the array every time you add an item - instead it doubles it when needed.

Try:

class Student
{
    IList<Subject> subjects = new List<Subject>();
}

class Subject
{
    string Name;
    string referenceBook;
}

Now you can say:

someStudent.subjects.Add(new Subject());

Comments

5

If you can, use a List<Subject> instead of Subject[]... this will let you do Student.Subject.Add(new Subject()). If that is not possible, you'll have to resize your array... look at Array.Resize() at http://msdn.microsoft.com/en-us/library/bb348051.aspx

Comments

2

I know this is old, but came across it looking for a simpler way, and this is how i do it, just create a new list of the same object and add it to the one you want to use e.g.

Subject[] subjectsList = {new Subject1{....}, new Subject2{....}, new Subject3{....}} 
univStudent.subjects = subjectsList ;

Comments

1

You can convert it ToList(), add and then convert it back ToArray().

var subjectList = univStudent.subjects.ToList();
subjectList.Add(new Subject());
univStudent.subjects = subjectList.ToArray();

Comments

1

You can create a list as follows.

List<Subject> allDestek = new List<Subject>() {
   new Subject(){ ID = 1, Name = "aaaa"},
   new Subject(){ ID = 2, Name = "bbbb"},
   new Subject(){ ID = 2, Name = "cccc"},
   new Subject(){ ID = 2, Name = "dddd"}
 };

 allSubject.ToArray()

If data is coming from somewhere (EF,SQL), you can add it in a table as follows.

Stutent[] allStudent = _dataRepository.Subjectes.ToArray();

Comments

-1

`

`Here is add method in student class to add subject in student object. I also make an subject object inside student object to add in ADD function and 
I change array of subject into List of subjects.````



class Student
{
   List<Subject> subjects new List<Subject>();

public static addSubject(string name, string referenceBook){
Subject subject = new Subject();
subject.name=name;
subject.referenceBook=referenceBook;
subjects=Add(subject);
System.Console.WriteLine($"Added Subject: {name} with referenceBook: {referenceBook}");


}

class Subject
{
    string Name;
    string referenceBook;
}
class Program
{
    static void Main(string[] args)
    {

        Student student = new Student();
        Subject subject = new Subject();
       
System.Console.WriteLine(" Enter Subject name:");
                    subject.name = Console.ReadLine();
                    System.Console.WriteLine(" Enter Subject Reference Book:");
                    subject.referenceBook = Convert.ToDouble(Console.ReadLine());
                    
                    student.addSubject(subject.name, subject.referenceBook);
          }
}

Comments

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.