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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:53:17+00:00 2026-05-10T15:53:17+00:00

Is there a way to collect (e.g. in a List) multiple ‘generic’ objects that

  • 0

Is there a way to collect (e.g. in a List) multiple ‘generic’ objects that don’t share a common super class? If so, how can I access their common properties?

For example:

class MyObject<T> {    public T Value { get; set; }    public string Name { get; set; }     public MyObject(string name, T value)    {       Name = name;       Value = value;    } }  var fst = new MyObject<int>('fst', 42); var snd = new MyObject<bool>('snd', true);  List<MyObject<?>> list = new List<MyObject<?>>(){fst, snd};  foreach (MyObject<?> o in list)    Console.WriteLine(o.Name); 

Obviously, this is pseudo code, this doesn’t work.

Also I don’t need to access the .Value property (since that wouldn’t be type-safe).

EDIT: Now that I’ve been thinking about this, It would be possible to use sub-classes for this. However, I think that would mean I’d have to write a new subclass for every new type.


@Grzenio Yes, that exactly answered my question. Of course, now I need to duplicate the entire shared interface, but that’s not a big problem. I should have thought of that…

@aku You are right about the duck typing. I wouldn’t expect two completely random types of objects to be accessible.

But I thought generic objects would share some kind of common interface, since they are exactly the same, apart from the type they are parametrized by. Apparently, this is not the case automatically.

  • 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. 2026-05-10T15:53:18+00:00Added an answer on May 10, 2026 at 3:53 pm

    I don’t think it is possible in C#, because MyObject is not a baseclass of MyObject. What I usually do is to define an interface (a ‘normal’ one, not generic) and make MyObject implement that interface, e.g.

    interface INamedObject {     string Name {get;} } 

    and then you can use the interface:

    List<INamedObject> list = new List<INamedObject>(){fst, snd};  foreach (INamedObject o in list)    Console.WriteLine(o.Name); 

    Did it answer your question?

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

Sidebar

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • 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 Use of Thumb will solve the problem. Just as <ListView… May 11, 2026 at 7:09 pm
  • Editorial Team
    Editorial Team added an answer It appears that you cannot do an update on a… May 11, 2026 at 7:09 pm
  • Editorial Team
    Editorial Team added an answer If you have an object (or a property of that… May 11, 2026 at 7:09 pm

Related Questions

Is there a way to collect (e.g. in a List) multiple 'generic' objects that
Suppose that I have a Java program within an IDE (Eclipse in this case).
My agile team will be adding new features to a existing realty website. As
I am using .net 1.1. I have a session class in which I have
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();

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.