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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:44:01+00:00 2026-05-28T07:44:01+00:00

My magic syntax would look like this: public List<T> LoadItems(string fileName) where T :

  • 0

My magic syntax would look like this:

public List<T> LoadItems(string fileName) where T : new (Dictionary<string,string>)
{
   ...
   List<T> List = new List<T>();
   while(reader.peek() != -1)
   {  
       Dictionary<string, string> NameValuePairs = new Dictionary<string,string>();
       ...
       //parse file and load dictionary with name value pairs
       ...
       List.Add(new T(NameValuePairs));

   }
} 

I would then like to be able to call this function like this…

public List<Class1> LoadClass1()
{
    return LoadItems<Class1>("file1.csv");
}

public List<Class2> LoadClass1()
{
    return LoadItems<Class2>("file2.csv");
}

public LisT<Class3> LoadClass1()
{
    return LoadItems<Class3>("file3.csv");
}

I realize that this can’t be done like this, but is there a way to implement something that works like this, perhaps using an interface for my three classes?

  • 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-05-28T07:44:01+00:00Added an answer on May 28, 2026 at 7:44 am

    If your classes, Class1, Class2, Class3, all take a dictionary as a constructor parameter, you could have them inherit from a common base class and use Activator.CreateInstance(typeof(T), NameValuePairs) to create instances of your classes.

    If you go that route, you’ll want to make your generic constraint where T : BaseClass and do some error checking/exception handling around your type creation.

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

Sidebar

Related Questions

I would like to implement a text editor with syntax highlighting, how the syntax
I would like to know what the best Scala imitation of Groovy's safe-dereference operator
Is there any particular reason to deny the use of magic constants (like __DIR__
Today I have seen this weird magic NTFS system supports: each file can have
I have several domains that I would like to have wildcard subdomains enabled for
I'm still new to String Processing in PHP. Below is a diagram of what
What syntax, if any, is able to take a reference of a builtin like
I came across this line in some code and can't find the syntax defined
Quite often, Git and Ruby on Rails looks like magic... such as in the
I need some jQuery magic that will look for all text on a page

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.