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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:35:03+00:00 2026-06-07T04:35:03+00:00

Given a generic attribute class: public class Attribute<T> { string Name { get; set;

  • 0

Given a generic attribute class:

public class Attribute<T>
{
  string Name { get; set; }
  T Value { get; set; }
}

Is there a way to have an object that contains an enumerable containing various types of that generic (for example int, string, datetime).

public class AttributableObject
{
  IEnumerable<???> attributes { get; set;}
}

Is there anyway to do this that won’t run into variance problems?

  • 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-06-07T04:35:05+00:00Added an answer on June 7, 2026 at 4:35 am

    The typical approach is to have the generic class derive from a non-generic base class (or interface,) and then have an enumerable of that type:

    public abstract class AttributeBase
    {
        // Non-generic stuff here
    }
    
    public class Attribute<T> : AttributeBase
    {
        // Generic stuff here
    }
    

    Then you can declare:

    IEnumerable<AttributeBase> attributes;
    

    Of course you’ll still need to know what the type of each element is if you’d like to use any of the generic features (like actually getting the value.)

    Note that covariance/contravariance won’t help here, since the Value property is both settable and gettable, and thus the generic parameter is invariant (not to mention that it’s a generic class, so it can’t be made co/contra-variant anyway.)

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

Sidebar

Related Questions

So I have a class that is a generic and it may need to,
So basically i have a domain object and a generic repository that can do
Given the following declaration: <Extension()> Public Function ToJSON(ByVal target As Object) As String Dim
Given an interface or interfaces, what is the best way to generate an class
This code outputs True . using System; using System.Collections.Generic; public class Default { public
I have a generic interface for a mathematics library, something like this: [ContractClass(typeof(MathsDoubleContracts))] public
Given a generic tree based (Java) data model. Let's say abstract class XModel {
I'm trying to create a UserControl that inherits from a generic class. It does
I have two models, Room and Image . Image is a generic model that
I have an application that saves its context to XML. In this application, there

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.