0

I have a c# question that im struggling with. I am designing a class but im not familiar with oop too much. Is it possible to define a list of the same type as the type of the class itself? For example say i have a dog class:

public class Dog
{
    public string _name {get; set;};

    public Dog (string, name)
    {
        _name = name;
    }

    public List<Dog> listOfDogs ()
    {
        // blah blah
    }
}

Is such a structure possible? if so, how? Is it the proper way to do something like this? Or should i create another helper class that simply does the building of the list of dogs by creating separate dog objects?

Thanks

Arthur
  • 7,939
  • 3
  • 29
  • 46
John Baum
  • 3,183
  • 11
  • 42
  • 90

4 Answers4

0

Is it possible to define a list of the same type as the type of the class itself?
Is such a structure possible?

Yes, it's perfectly legal. The question is whether it makes sense in the context of your application.

if so, how? Is it the proper way to do something like this?

The way you did it is fine, although typically it would be declared as a property rather than a method.

Or should i create another helper class that simply does the building of the list of dogs by creating separate dog objects?

It depends on what you want to do... It's hard to tell what is your goal exactly.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
0

Short answert: Yes, it is.

This makes sense in many cases. A Dog could be associated to other dogs in an OOP world.

Besides, let me just suggest you a few other tiny improvements:

public class Dog
{
  public string Name { get; set; } // PascalCase is a convention here

  public Dog(string name)
  {
    Name = name;
  }

  public IList<Dog> listOfDogs () // try to expose interfaces whenever possible
  {
    // blah blah
  }
}

Especially the interface thing is worth a lot of reading if you are new to OOP. There are a few different list implementations, and OOP helps you to hide these so-called "implementation details". Later on you could use a Dog[] instead of a List<Dog> without changing consumers of your class. Both do implement IList<Dog>.

It's stuff that is described in detail in many places. There are books, articles, or this question on SO: What do programmers mean when they say, "Code against an interface, not an object."?

Community
  • 1
  • 1
Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
0

Do this:

public class Dogs : List<Dog> {
   // your additional methods/properties/etc go here
   // every object of this list will be of the type "Dog"
   // and you cannot add any type other than the type "Dog" to this list
}

Hope that helps

EDIT: I hope I understood your question correctly...

You might also want to change the definition of you class "Dog" to this:

public class Dog {
    public Dog(string name) {
        this.Name = name;
    }

    public string Name { get; set; }
}
MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
0

Yes, it is perfectly legal for a class to refer to itself and have instances of itself as members.

Nevertheless this is probably not the design you want for this specific problem. It is not logical (in my mind) for a dog to have a list of other dogs.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222