views:

110

answers:

5

Ohk, I am doing a c# program, and I get everything but this, I just can't understand what it is asking. I know how to create a list.. and how to create a constructor..

but this is where i get confused.. its probably way simple but i am missing it.

I created 2 lists .. now i should create a constructor

here is one my lists

List<Person> organize = new List<Person>();

THIS PART ---> *The constructor should also initialize the two event lists to new empty lists. *

A: 
List<Person> organize = new List<Person>();

This will create an empty list. Can you be more specific? I feel like some details are missing.

Perhaps you mean something like this?

 public class TestClass
 {
      List<Person> personList;

      public TestClass()
      {
           personList = new List<Person>();
      }

 }
Bryan Denny
+8  A: 

Based on what I can gather from your question, you have a class with two Lists.

Your requirements say that inside your class, you need to initialize the Lists to empty lists.

Below is the example (the only difference is that I never initialize the Lists when they declared, but in the class constructor instead):

public class YourClass
{
    List<Person> organize;
    List<Person> anotherOrganize;

    // constructor
    public YourClass()
    {
        // initialize the two lists to empty lists
        organize = new List<Person>();
        anotherOrganize = new List<Person>();
    }
}
Justin Niessner
Nice and clean example
Jamie Keeling
A: 

I think what you're asking is why the constructor to your class should instantiate the two lists (?). You're right that

List<Person> organize = new List<Person>();

will instantiate the list. But so will:

List<Person> organize;
public MyClass()
{
organize = new List<Person>();
}

Am I understanding the question?

statichippo
+1  A: 

If your list is declared as a field (member variable directly inside the class) and it's initialized at its declaration, you shouldn't to reinitialize it in the constructor. The initialization expression will get moved to the constructor by the compiler automatically.

Mehrdad Afshari
+1  A: 

If this is homework, your instructor is probably telling you to initialize the lists inside of the constructor instead of at the declaration point.

class MyClass
{
    List<Person> organize;

    public MyClass()
    {
        this.organize = new List<Person>();
    }
}
NickLarsen