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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:58:10+00:00 2026-05-12T19:58:10+00:00

I am trying to create a generic class which new’s up an instance of

  • 0

I am trying to create a generic class which new’s up an instance of the generic type. As follows:

public class HomepageCarousel<T> : List<T>
    where T: IHomepageCarouselItem, new()
{
    private List<T> GetInitialCarouselData()
    {
        List<T> carouselItems = new List<T>();

        if (jewellerHomepages != null)
        {
            foreach (PageData pageData in jewellerHomepages)
            {
               T item = new T(pageData); // this line wont compile
               carouselItems.Add(item);
            }
        }
        return carouselItems;
    }
}

But I get the following error:

cannot provide arguments when creating an instance of a variable
type

I found the following related question which is very close to what I need:
Passing arguments to C# generic new() of templated type

However, I can’t used Jared’s suggested answer as I am
calling the method within the Generic class, not outside of
it, so I can’t specify the concrete class.

Is there a way around this?

I have tried the following based on the other question, but
it doesn’t work as I don’t know the concrete type of T to
specify. As it is called from inside the generic class, not
outside:

public class HomepageCarousel<T> : List<T>
    where T: IHomepageCarouselItem, new()
{

    private List<T> LoadCarouselItems()
    {
        if (IsCarouselConfigued)
        {
            return GetConfiguredCarouselData();
        }

        // ****** I don't know the concrete class for the following line,
        //        so how can it be instansiated correctly?

        return GetInitialCarouselData(l => new T(l));
    }

    private List<T> GetInitialCarouselData(Func<PageData, T> del)
    {
        List<T> carouselItems = new List<T>();

        if (jewellerHomepages != null)
        {
            foreach (PageData pageData in jewellerHomepages)
            {
                T item = del(pageData);
                carouselItems.Add(item);
            }
        }
        return carouselItems;
    }
}

********EDIT : ADDED POSSIBLE SOLUTIONS**

So I have tested 2 possible solutions:

First is exactly as explained below by Jon Skeet. This
definitely works but means having an obscure lambda in the
constructor. I am not very comfortable with this as it means
users need to know the correct lambda that is expected.
After all, they could pass a lambda which doesn’t new up the
type, but does something entirely unexpected

Secondly, I went down the Factory method route;
I added a Create method to the common interface:

IJewellerHomepageCarouselItem Create(PageData pageData);

Then provided an implementation in each Concrete class:

public IJewellerHomepageCarouselItem Create(PageData pageData)
{
     return new JewellerHomepageCarouselItem(pageData, null);
}

And used a two step initialisation syntax:

T carouselItem = new T();
T homepageMgmtCarouselItem = (T) carouselItem.Create(jewellerPage);

Would love to hear some feedback on the merit of each of these approaches.

  • 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-12T19:58:10+00:00Added an answer on May 12, 2026 at 7:58 pm

    Jared’s answer is still a good way to go – you just need to make the constructor take the Func<PageData, T> and stash it for later:

    public class HomepageCarousel<T> : List<T> where T: IHomepageCarouselItem
    {
        private readonly Func<PageData, T> factory;
    
        public HomepageCarousel(Func<PageData, T> factory)
        {
            this.factory = factory;
        }
    
        private List<T> GetInitialCarouselData()
        {
           List<T> carouselItems = new List<T>();
    
           if (jewellerHomepages != null)
           {
                foreach (PageData pageData in jewellerHomepages)
                {
                    T homepageMgmtCarouselItem = factory(pageData);
                    carouselItems.Add(homepageMgmtCarouselItem);
                }
           }
           return carouselItems;
        }
    

    Then you just pass the function into the constructor where you create the new instance of the HomepageCarousel<T>.

    (I’d recommend composition instead of inheritance, btw… deriving from List<T> is almost always the wrong way to go.)

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

Sidebar

Ask A Question

Stats

  • Questions 240k
  • Answers 240k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's possible to change the header of the Dialog if… May 13, 2026 at 7:10 am
  • Editorial Team
    Editorial Team added an answer I would recommend using Clang Static Analyzer to check your… May 13, 2026 at 7:10 am
  • Editorial Team
    Editorial Team added an answer Simply checkout from your repository, and you'll get the complete… May 13, 2026 at 7:10 am

Related Questions

I am trying to create a generic class which new's up an instance of
I have the following code, where i am trying to create a generic collection
I am trying to use Generic Types in Windows.Resources section in XAML code. To
I've been trying to solve this for ages (3 days) now and I just
I have a persistence framework, and I am trying to use generics so I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.