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 899687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:15:37+00:00 2026-05-15T15:15:37+00:00

I need a base class with a property where I can derive classes with

  • 0

I need a base class with a property where I can derive classes with the same property but different (compatible) types. The base Class can be abstract.

public class Base
{
    public virtual object prop { get; set; }
}

public class StrBase : Base
{
    public override string prop { get; set; } // compiler error
}

public class UseIt
{
    public void use()
    {
        List<Base> l = new List<Base>();
        //...
    }
}

I tried it with Generics but that gives me a problem when using the class, because I want to store differently typed base classes in the List.

public class BaseG<T>
{
    public T prop { get; set; }
}

public class UseIt
{
    public void use()
    {
        List<BaseG> l = new List<BaseG>(); // requires type argument
        //...
    }
}
  • 1 1 Answer
  • 1 View
  • 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-15T15:15:38+00:00Added an answer on May 15, 2026 at 3:15 pm

    Here’s an alternative approach to proposed solution:

    public abstract class Base
    {
        public abstract void Use();
        public abstract object GetProp();
    }
    
    public abstract class GenericBase<T> : Base
    {
        public T Prop { get; set; }
    
        public override object GetProp()
        {
            return Prop;
        }
    }
    
    public class StrBase : GenericBase<string>
    {
        public override void Use()
        {
            Console.WriteLine("Using string: {0}", Prop);
        }
    }
    
    public class IntBase : GenericBase<int>
    {
        public override void Use()
        {
            Console.WriteLine("Using int: {0}", Prop);
        }
    }
    

    Basically I’ve added a generic class in the middle that stores your properly-typed property. this will work assuming that you never need to access Prop from the code that iterates the members of the List<Base>. (You could always add an abstract method to Base called GetProp that casts the generic to an object if that’s required.)

    Sample usage:

    class Program
    {
        static void Main(string[] args)
        {
            List<Base> l = new List<Base>();
    
            l.Add(new StrBase {Prop = "foo"});
            l.Add(new IntBase {Prop = 42});
    
            Console.WriteLine("Using each item");
            foreach (var o in l)
            {
                o.Use();
            }
            Console.WriteLine("Done");
            Console.ReadKey();
        }
    }
    

    Edit: Added the GetProp() method to illustrate how the property can be directly accessed from the base class.

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

Sidebar

Ask A Question

Stats

  • Questions 442k
  • Answers 442k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If it's just an HTML drop-down list (a "select" tag),… May 15, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer Yes: $ echo -n "363" | md5sum 00411460f7c92d2124a67ea0f4cb5f85 - $… May 15, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer You could do like so: $('#freeQuote form').submit(function(e){ //Set the data… May 15, 2026 at 5:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.