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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:14:35+00:00 2026-05-31T12:14:35+00:00

What differences are there between declaring: GenericClass<T> genericInst = new GenericClass<T>(); and GenericClass<baseClass> temp

  • 0

What differences are there between declaring:

GenericClass<T> genericInst = new GenericClass<T>();

and

GenericClass<baseClass> temp = new GenericClass<baseClass>();

Here the GenericClass is defined to be for where T : baseClass

GenericClass contains a generic list

private List<T> vals = new List<T>();
  • 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-31T12:14:36+00:00Added an answer on May 31, 2026 at 12:14 pm

    It seems to me, and please correct me if I’m wrong, that you are taking ‘where T : baseClass’ as if it were a default for type T to be a baseClass?

    If so, this is not the case, the specialization where T : baseClass means that the type T must be baseClass or derived from baseClass (or implement if it were an interface instead of a class).

    Thus, if you had:

    public class GenericClass<T> where T : baseClass
    {
    }
    

    Then you can say:

    var x = new GenericClass<baseClass>();
    

    Or

    var y = new GenericClass<SomethignDerivedFromBaseClass>();
    

    But you could not say:

    var z = new GenericClass<int>();
    

    Since int does not inherit from baseClass.

    The only way to actually use T in the instantiation above is if you were actually calling that line of code from within the GenericClass<T>:

    public GenericClass<T> where T : baseClass
    {
        void SomeMethod()
        {
            GenericClass<T> genericInst = new GenericClass<T>();
        }
    }
    

    Or from another context where T is already known to be a sub-class of baseClass.

    UPDATE

    Based on your comments, it sounds like you’re wondering that if you had:

    public class GenericClass<T> where T : baseClass
    {
        public List<T> Items { get; set; }
    
        ...
    }
    

    Whether you could add things derived from baseClass into the list, and the answer is yes-ish. The class GenericClass<T> could be declared for any T that inherits from baseClass. But the List<T> would still be strongly typed to type T.

    That is given these:

    public class BaseClass { }
    public class SubClass : BaseClass { }
    

    A GenericClass<BaseClass> could hold both BaseClass and SubClass in it’s List<T> since T will be BaseClass.

    But a GenericClass<SubClass> will have a List<T> where T will be SubClass and thus can only hold items of SubClass or inheriting from it.

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

Sidebar

Related Questions

Is there a list of the differences between the various versions of Rails somewhere
Are there any important differences between declaring <script type=text/javascript> </script> and <script language=javascript> </script>
What differences are there between Windows and Console applications ? When creating a new
Is there any differences between : > file and > file ? $ :
Are there any differences between get_object_vars($obj) and (array) $obj ? Both seem to return
What are the differences between a Just-in-Time-Compiler and an Interpreter, and are there differences
Are there any differences when it comes to performance between the following three border
Is there any performance differences between string.Format and String.Format ? As far as i
Anyone out there who knows what are the differences between BasicRenderEngine and LazyRenderEngine?
What are the differences between these two codes in JavaScript? var obj = new

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.