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

The Archive Base Latest Questions

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

Should’t this be valid C# code? class A<T> where T : class { public

  • 0

Should’t this be valid C# code?

class A<T> where T : class {

    public void DoWork<K>() where K : T {

        var b = new B<K>(); // <- compile time error
    }
}

class B<U> where U : class {

}

The compiler spits this error:

error CS0452: The type ‘K’ must be a reference type in order to use it as parameter ‘U’ in the generic type or method ‘ConsoleApplication1.B’

Shouldn’t the compiler be able to figure out that K is constraint to be of type T or derived from T so it should obviously be a reference type (T is constrained to be a reference type)?

  • 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-20T05:14:27+00:00Added an answer on May 20, 2026 at 5:14 am

    The constraints are applied when the type parameter is specified. A type has not been specified for K even though K is being specified for U. As U requires its type to be a reference type, the compiler is looking to confirm that K is indeed a reference type, but it cannot. Therefore, you need to explicitly state that it will be.

    The specification states in section 4.4.4:

    For each where clause, the type argument A that corresponds to the named type parameter is checked against each constraint…

    and later:

    A compile-time error occurs if one or more of a type parameter’s constraints are not satisfied by the given type arguments.

    Since type parameters are not inherited, constraints are never inherited either.

    This last point indicates that K will not inherit the constraints from T.

    Update
    While my conclusions appear correct, my evidence is a little shaky as was clarified in a now deleted response from Eric Lippert’s response. There, Eric stated that the correct part of the specification is:

    A type parameter is known to be a
    reference type if it has the reference
    type constraint or its effective base
    class is not object or
    System.ValueType.

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

Sidebar

Related Questions

Should 'this' in the following code not still refer to the DOM object selected
Should I still be using tables anyway? The table code I'd be replacing is:
Should I catch exceptions for logging purposes? public foo(..) { try { ... }
Should I keep all the articles in rss feed or just new ones? There
Should I be using alert() for debugging; or is there a time to use
Should I be using .htaccess or is this something in the httpd.conf file? or
Should I start with Django or JavaScript?
Should the folders in a solution match the namespace? In one of my teams
Should you set all the objects to null ( Nothing in VB.NET) once you
Should I use Named Pipes, or .NET Remoting to communicate with a running process

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.