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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:50:39+00:00 2026-06-06T17:50:39+00:00

I’ve run into a little difficulty and I’m not sure how best to solve

  • 0

I’ve run into a little difficulty and I’m not sure how best to solve this issue.

I wrote a class that expects data type to be specified upon the creation of a new instance. Much like in the same way as the System.Collections.Generic.List<T> class that stores a number of items of a specified type. However my class doesn’t has the same or similar function as the List<T> class does.

Upon creation of a new instance of my class, for example:

string phrase = "Some string type data.";
MyClass<string> data = new MyClass<string>( () => phrase );

Afterwards my class would provide a number of methods that would give a result regardless of the type of data being imparted into MyClass<T> (where T is the data type). Doesn’t matter if it is string, bool, int, or any kind.

I’ve tested my class and it works as expected, now how can I store a number of elements of MyClass but each element with different imparted data type, inside a collection class such as List<>.

Let me exemplify (code will contain syntax errors since I’m not sure how to write correctly):

// My intent... Not sure how to do it.
string phrase = "Some string data type.";
int number = 23;
bool boolData = false;

List<MyClass<T>> list = new List<MyClass<T>>();

list.Add( new MyClass<string>( () => phrase ) );
list.Add( new MyClass<int>( () => number ) );
list.add( new MyClass<bool>( () => boolData ) );

As you see I don’t know how to store a number of element of MyClass with different imparted data types, if they were all the same imparted data type, I wouldn’t be having this issue.

For example:

// How it should be done. But not what I need.
string phrase1 = "Hello Phrase 1.";
string phrase2 = "Hello Phrase 2.";

List<MyClass<string>> list = new List<MyClass<string>>();

list.Add(new MyClass<string>( () => phrase1) );
list.Add(new MyClass<string>( () => phrase2) );

If this was what I wanted to accomplish, I wouldn’t have a problem, but since I want to accomplish what I exemplified before this last example, I’m just not sure how to best do it.

Basically thats my issue, I’ve provided a few basic examples because I believe my question might be a bit odd, and to be honest my terminology wouldn’t not be adequate I think.

This is probably quite simple for some of you. My thanks for your help.

  • 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-06T17:50:40+00:00Added an answer on June 6, 2026 at 5:50 pm

    Typically you’d create a non-generic base class (or interface) for the generic type:

    abstract class MyClass // Or interface
    {
        // Anything useful but non-generic here
    }
    
    class MyClass<T> : MyClass
    {
        // Anything using T
    }
    

    Then you can just create a List<MyClass>. Of course to get at any of the members within MyClass<T> you’ll need to know the right T to cast to, but I think that’s unavoidable.

    Of course this doesn’t help much if you don’t control MyClass<T> and it doesn’t provide a useful base class – but there’s nothing in C# to help you with this situation.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I know there's a lot of other questions out there that deal with this
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.