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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:30:22+00:00 2026-06-12T14:30:22+00:00

I have an interface ISnack which when implemented by a class, it should have

  • 0

I have an interface ISnack which when implemented by a class, it should have a default parameterless constructor. Basically this:

public interface ISnack<T> where T : new()
{

}

I use <T> where T : new() just to enforce the parameterless constructor.

I would then implement the interface this way:

public class Cutlet : ISnack<Cutlet>
{

}

This works and it simply ensures Cutlet class has a parameterless constructor.

Now I have an abstract base class Kitchen:

public abstract class Kitchen<T> where T : ISnack
{

}

The requirement is that Kitchen should have constraint where T should be an ISnack. But this wont work because there exists no ISnack, but only ISnack<T>.

If I tried this

public abstract class Kitchen<T> where T : ISnack<T>
{

}

it wouldn’t compile ('T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'ISnack<T>') and also wouldn’t make sense in my context.

If I could force ISnacks to have a parameterless constructor without constraining by a T type parameter, then T in Kitchen<T> could easily be an ISnack. How to go about it?

  • 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-12T14:30:24+00:00Added an answer on June 12, 2026 at 2:30 pm

    You can’t unless you add the constraint; generic constraints are cumulative, so to make the compiler happy you would have to have:

    public abstract class Kitchen<T> where T : ISnack<T>, new()
    

    If that is fine, then do that. If it isn’t fine, then you’ll have to remove the : new from the original, and make do without it. This isn’t as bad as it sounds, but it means you push validation down to execution rather than compilation. But: Activator.CreateInstance<T>() still does what you would need, anyway – even without the new() constraint. So you can replace:

    T newObj = new T(); // validated by the compiler
    

    with:

    T newObj = Activator.CreateInstance<T>(); // not validated until executed
    

    A handy trick when removing constraints can be: add a unit/integration test that finds the candidate types via reflection, and validate the missing constraint as part of your test suite.

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

Sidebar

Related Questions

I have interface Foo public interface Foo { public void test(); } Class FooChild
I have an Interface like this: namespace QuickRoutes.Model.Utilities { public interface IRoutesManager { bool
I have next interface public interface IMyInterface { string this[string key] { get; set;
Assume I have interface and class: public interface ITree {} public class Tree :
i have interface: public interface Inx<T> { T Set(Data data); } simple class with
I have interface based on another: class IDrawable { public: virtual ~IDrawable(); }; class
I have seen that if I have interface named interfaceABC. Example: public class ABController
If I have interface and implementing classes like public interface IA {} public class
I have interface IConfigurationSource { .... } and class FileConfigurationSource : IConfigurationSource { FileConfigurationSource(string
I'm trying to figure out why this code won't compile. I have interface A

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.