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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:26:31+00:00 2026-05-11T05:26:31+00:00

I have the following situation: // A public interface of some kind public interface

  • 0

I have the following situation:

// A public interface of some kind    public interface IMyInterface {        int Something { get; set; }    }     // An internal class that implements the public interface.    // Despite the internal/public mismatch, this works.    internal class MyInternalConcrete : IMyInterface {        public int Something { get; set; }    }     // A generic class with an interface-restricted type parameter. // Note that the constraint on T uses the *public* interface. // The instance is *never* exposed as a public, or even protected member. public class MyClass<T> where T : IMyInterface, new() {        T myInterfaceInstance;         public MyClass() {            myInterfaceInstance = new T();        }    }     // Attempting to implement concrete class... Inconsistent Accessibility Error!    public class MySpecificClass : MyClass<MyInternalConcrete>    {    }   

When trying to implement MySpecificClass, I get the error:

Inconsistent accessibility: base class ‘App1.MyClass’ is less accessible than class ‘App1.MySpecificT’

Where it gets weird is that MyInternalConcrete, despite being internal, can still implement a public interface. And since it implements the interface, then it should be useable as a type parameter for MyClass – because T is constrained on the public interface and not the internal class.

I would understand it failing if MyClass exposed T, just as it would fail if we weren’t using generics:

public class MyClass<T> where T : IMyInterface, new() {           T myInterfaceInstance;            public MyClass() {               myInterfaceInstance = new T();           }            // This will fail with an internal T - inconsistent accessibility!         public T Instance {               get { return myInterfaceInstance; }           }       } 

And same as above, but without generics:

public class MyNonGenericClass {        MyInternalConcrete myInterfaceInstance;         public MyNonGenericClass() {            myInterfaceInstance = new MyInternalConcrete();        }         // This will fail - inconsistent accessibility!      // but removing it works, since the private instance is never exposed.        public MyInternalConcrete Instance {            get { return myInterfaceInstance; }        }    }   

Is this a limitation of the C# generics or am I simply misunderstanding something fundamental about how generics work?

I also posted this thread on MSDN, but I’m being dismissed as not knowing what I’m talking about. Is my concern even valid?

  • 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-11T05:26:31+00:00Added an answer on May 11, 2026 at 5:26 am

    This constraint you are facing makes sense for the following reason.

    C# is strongly typed so…

    To be able to reference the MySpecificClass outside the scope of the assembly it is defined in you must know its parameter types in order to generate a strong type reference to its instance; but an separate assembly than the internal definition does not know about MyInternalConcrete.

    Thus the following wont work if in a separate assembly:

    MyClass<MyInternalConcrete> myInstance = new MySpecificClass(); 

    Here the separate assembly doesn’t know of MyInternalConcrete, so how can you define a variable as such.

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

Sidebar

Related Questions

I have following component composition: public interface IJob { ILogger Logger { get; set;
I have the following situation: class A { public: A(int whichFoo); int foo1(); int
Suppose you have the following situation #include <iostream> class Animal { public: virtual void
I have the following situation: typedef void (*F_POINTER)(ClassName*); class ClassName { public: F_POINTER f;
I have the following situation. I have a Java Class that inherits from another
I have following class: public class PairOfDice { private Dice d1,d2; public int Value
I have following situation: public interface IFormater { StyleInformation GetStyleInformation(Fact fact); FactType[] Formats {
I have the following situation: Interface public interface Ranged{ public Object getRangeStart(); public Object
I have the following situation class B { public: B() {}; virtual ~B() {};
I have following situation - Have a MongoService class, which reads host, port, database

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.