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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:56:18+00:00 2026-06-03T21:56:18+00:00

public abstract Column<T> { private T Value {get;set;} public abstract string Format(); } public

  • 0
public abstract Column<T>
{
   private T Value {get;set;}    

   public abstract string Format();

}

public class DateColumn : Column<DateTime>
{
   public override string Format()
   {
      return Value.ToString("dd-MMM-yyyy");
   }
}

public class NumberColumn : Column<decimal>
{
   public override string Format()
   {
      return Value.ToString();
   }
}

The problem I have is adding these into a generic collection. I know its possible but how can I store multiple types in a collection etc

IList<Column<?>> columns = new List<Column<?>()

I would really appreciate any advice on achieving this goal. The goal being having different column types stored in the same List. Its worth mentioning I am using NHibernate and the discriminator to load the appropriate object.Ultimately the Value needs to have the type of the class.

Many thanks for your help in advance.

  • 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-03T21:56:20+00:00Added an answer on June 3, 2026 at 9:56 pm

    In order to be stored in a List<T> together the columns must have a common base type. The closest common base class of DateColumn and NumberColumn is object. Neither derives from Column<T> but instead a specific and different instantiation of Column<T>.

    One solution here is to introduce a non-generic Column type which Column<T> derives from and store that in the List

    public abstract class Column { 
      public abstract object ValueUntyped { get; }
    }
    
    public abstract class Column<T> : Column {
      public T Value { get; set; }
      public override object ValueUntyped { get { return Value; } }
    }
    
    ...
    
    IList<Column> list = new List<Column>();
    list.Add(new DateColumn());
    list.Add(new NumberColumn());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public abstract class HolidayPackageVariant { private HolidayPackage holidayPackage; private String typeHolidayPackage; @Override public int
classes: public abstract class BaseHolidayPackageVariant { private Integer variantId; private HolidayPackage holidayPackage; private String
public abstract class Entity : IEntity { [Key] public virtual int Id { get;
I have the following abstract class: public abstract class TemplateBase { public abstract string
I have 4 classes as below... @MappedSuperclass @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstract class Platform{ @Id @Column(name
public abstract class Master { public void printForAllMethodsInSubClass() { System.out.println (Printing before subclass method
public abstract class MyAbs implements Comparable<MyAbs> This would work but then I would be
public abstract class Request { public class Parameters { //Threre are no members here
I have an interface: public abstract class Authorizer<T> where T : RequiresAuthorization { public
Function abstraction: public abstract class Function<X, Y> { abstract Y apply(X x); } max

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.