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

  • Home
  • SEARCH
  • 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 6202533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:45:07+00:00 2026-05-24T04:45:07+00:00

Let’s say I have a base class named BaseClass: public BaseClase{ public bool isCool;

  • 0

Let’s say I have a base class named BaseClass:

public BaseClase{
    public bool isCool;
}

Now let’s say I have two classes that inherit from BaseClass

public Class1: BaseClass{
    public bool isGreen;
}

public Class2: BaseClass{
    public bool isPurple;
}

Now let’s say I want to create a list that contains instances of both Class1 and Class2 by creating a List;

var genericList = new List<BaseClass>();

Next, let’s add an instance of Class1 and Class2 to genericList.

genericList.Add(new Class1());
genericList.Add(new Class2());

Now, here is what I have a question about. If I want to access Class1.IsGreen or Class2.IsPurple, I have to cast each item in genericList as Class1 or Class2.

foreach(var item in genericList){
    if(item is Class1){
        var temp = (Class1) item;
        temp.IsGreen = true;
    }
    if(item is Class2){
        var temp = (Class2) item;
        item.IsPurple = true;
    }
}

Is this just the way you’re supposed to do things? It seems very clunky to me, and the complexity of the code I’m writing that uses this type of code structure is getting out of hand. I’m new to inheritance, and want to learn if this is just the way you’re supposed to do things, or if there are better alternatives out there.

  • 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-24T04:45:08+00:00Added an answer on May 24, 2026 at 4:45 am

    It really depends on what you are trying to do. If you can abstract out the property, so something like IsSelected, then you could expose it on your BaseClass as a virtual or abstract property. Then you wouldn’t have to cast your items in your for-loop.

    Or you could abstract it out to a abstract/virtual method, like UpdateColor(bool). Then each derived class could override that and set the appropriate property on themselves.

    There are several other alternatives, including interfaces and extension methods, that you could use to make it cleaner.

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

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say I have following POJO's using Hibernate. public class User { private String
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a class like so: class Order { const STATUS_INITIALIZED =
Let's say we have a template class Area , which has a member variable
Let's say you have a JavaScript class like this var DepartmentFactory = function(data) {
Let's say I have a main folder in my website named test which contains
Let's imagine I have a Java class of the type: public class MyClass {
Let say I have two UIViews: View1: - bounds: 0, 0, 20, 20 -

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.