Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:10:05+00:00 2026-05-30T23:10:05+00:00

I have a c# question that im struggling with. I am designing a class

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:10:06+00:00Added an answer on May 30, 2026 at 11:10 pm

    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."?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that is similar to this question , but mine concerns
I have a question that may be quite naive, but I feel the need
I have a question that I'm ashamed to ask, but I'm going to have
I have a question that may sound odd, but being somewhat of a newbie,
I have had a question that I actually know the answer to but wanted
I know this is a very simple question but I have been struggling with
I have a situation that I am struggling with conceptually. I have a class
I have a question that I'm struggling with in ADO.NET Data Services: When assembling
I have a question that keeps bothering me. Currently, I have started using Kohana
I have a question that I just don't feel like I've found a satisfactory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.