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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:35:24+00:00 2026-05-11T14:35:24+00:00

I am a bit rusty on generics, trying to do the following, but the

  • 0

I am a bit rusty on generics, trying to do the following, but the compiler complains:

protected List<T> PopulateCollection(DataTable dt) where T: BusinessBase {     List<T> lst = new List<T>();     foreach (DataRow dr in dt.Rows)     {         T t = new T(dr);         lst.Add(t);     }     return lst; } 

So as you can see, i am trying to dump contents of a Table into an object (via passing a DataRow to the constructor) and then add the object to collection. it complains that T is not a type or namespace it knows about and that I can’t use where on a non-generic declaration.

Is this not possible?

  • 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. 2026-05-11T14:35:24+00:00Added an answer on May 11, 2026 at 2:35 pm

    There are two big problems:

    • You can’t specify a constructor constraint which takes a parameter
    • Your method isn’t currently generic – it should be PopulateCollection<T> instead of PopulateCollection.

    You’ve already got a constraint that T : BusinessBase, so to get round the first problem I suggest you add an abstract (or virtual) method in BusinessBase:

    public abstract void PopulateFrom(DataRow dr); 

    Also add a parameterless constructor constraint to T.

    Your method can then become:

    protected List<T> PopulateCollection(DataTable dt)     where T: BusinessBase, new() {     List<T> lst = new List<T>();     foreach (DataRow dr in dt.Rows)     {         T t = new T();         t.PopulateFrom(dr);         lst.Add(t);     }     return lst; } 

    If you’re using .NET 3.5, you can make this slightly simpler using the extension method in DataTableExtensions:

    protected List<T> PopulateCollection<T>(DataTable dt)     where T: BusinessBase, new() {     return dt.AsEnumerable().Select(dr =>      {          T t = new T();         t.PopulateFrom(dr);     }.ToList(); } 

    Alternatively, you could make it an extension method itself (again, assuming .NET 3.5) and pass in a function to return instances:

    static List<T> ToList<T>(this DataTable dt, Func<DataRow dr, T> selector)     where T: BusinessBase {     return dt.AsEnumerable().Select(selector).ToList(); } 

    Your callers would then write:

    table.ToList(row => new Whatever(row)); 

    This assumes you go back to having a constructor taking a DataRow. This has the benefit of allowing you to write immutable classes (and ones which don’t have a parameterless constructor) it but does mean you can’t work generically without also having the ‘factory’ function.

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

Sidebar

Related Questions

I'm pretty new to Android but I have some experience (and a bit rusty
My C++ is a bit rusty so... #include<list> typedef list<int> foo; that gives me
I'm new to Python, and a bit rusty with my linear algebra, so perhaps
I am a bit rusty with mysql and trying to jump in again..So sorry
First of all I am new to python and a bit rusty on .NET
My SQL is a bit rusty -- is there a SQL way to project
I am a bit rusty on my Haskell and am looking to ramp back
My knowledge about implementing a parser is a bit rusty. I have no idea
After two years of C#, my VB.NET is a bit rusty. I have two
A bit of a neophyte haskell question, but I came across this example in

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.