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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:14:22+00:00 2026-06-17T15:14:22+00:00

I keep using the function below in my classes and would like to write

  • 0

I keep using the function below in my classes and would like to write it as generics.

public static IEnumerable<MyObject> Get(string csvFile)
{
    return csvFile
        .ReadAsStream()
        .SplitCrLf()
        .Where(row => !string.IsNullOrWhiteSpace(row))
        .Select(row => new MyObject(row.Split(',')));
}

I scratch the code below but didn’t work

public static IEnumerable<T> Get<T>(string csvFile)
{
    return csvFile
        .ReadAsStream()
        .SplitCrLf()
        .Where(row => !string.IsNullOrWhiteSpace(row))
        .Select(row => new typeof(T)(row.Split(',')));
}

Please advise. Thank you!

  • 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-17T15:14:23+00:00Added an answer on June 17, 2026 at 3:14 pm

    You cannot use new to create instances using generic types in this way1. Consider supplying a factory delegate to the function:

    public static IEnumerable<T> Get<T>(string csvFile, Func<string[], T> factory)
    {
        return csvFile
            .ReadAsStream()
            .SplitCrLf()
            .Where(row => !string.IsNullOrWhiteSpace(row))
            .Select(row => factory(row.Split(',')));
    }
    

    Then you would call it like so:

    var myObjects = Get("file.csv", row => new MyObject(row));
    

    Alternatively, you can return an IEnumerable<string[]>2 and let the caller decide what to do with it:

    public static IEnumerable<string[]> Get(string csvFile)
    {
        return csvFile
            .ReadAsStream()
            .SplitCrLf()
            .Where(row => !string.IsNullOrWhiteSpace(row))
            .Select(row => row.Split(','));
    }
    

    Then the caller could do:

    var myObjects = Get("file.csv").Select(row => new MyObject(row));
    

    1You can supply the where T : new() constraint and then you can create new instances using a generic type, but only when it provides a no-argument constructor; you cannot provide arguments when constructing generic types, and your use case appears to require it. A factory delegate is your best option here.

    For reference, this is how construction using generic types would look in the no-argument case:

    public static T Create<T>() where T : new()
    {
        return new T();
    }
    

    2Even better would be an IEnumerable<IEnumerable<string>> assuming that your MyObject constructor accepts IEnumerable<string> as well.

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

Sidebar

Related Questions

I using jQuery Ajax like below: $.ajax({ url: 'servlet/*****Servlet', dataType: text, success: function(data) {
When using the PHP Tidy Library (More Specifically, The tidy_repair_string function) I keep getting
The question I would like to ask is if the Javascript snippet below is
Am trying to get my 'Allocate auto every minute' button below to keep submitting
I have multiple divs like setted up below. I want 1 (one) JQuery function
I used a built in function to create comma separated string using List easily.
I have javascript code like below - function initPage(){ // Left Navigation Pane -
I am using the function below to capture an image on a different form.
When using VB.NET I can do the following: Public Function GetUser(ByVal ID as Integer)
I have some code like below in an aspx page: function CheckEditing() { var

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.