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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:05:09+00:00 2026-06-18T21:05:09+00:00

I hope the question is correct, so let’s give you an example. Imagine the

  • 0

I hope the question is correct, so let’s give you an example. Imagine the following generic method:

public abstract class Base : IDisposable
{
    public static IEnumerable<T> GetList<T>()
        where T : Base
    {
        // To ensure T inherits from Base.
        if (typeof(T) is Base)
            throw new NotSupportedException();

        // ...
    }
}

According to the MSDN the keyword where restricts the type parameter T to be of type Base or to inherit from this class.

[…] a where clause can include a base class constraint, which states that a type must have the specified class as a base class (or be that class itself) in order to be used as a type argument for that generic type.

Also this code does compile:

public static T GetFirst()
    where T : Base
{
    // Call GetList explicitly using Base as type parameter.
    return (T)GetList<Base>().First();
}

So when following the last code typeof(T) should return Base, shouldn’t it? Why does Visual Studio then prints this warning to me?

warning CS0184: The given expression is never of the provided (‘Demo.Base’) 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-06-18T21:05:15+00:00Added an answer on June 18, 2026 at 9:05 pm

    typeof(whatever) always returns an instance of type Type. Type doesn’t derive from Base.

    What you want is this:

    if(typeof(T) == typeof(Base))
        throw new NotSupportedException("Please specify a type derived from Base");
    

    Something that looks like it is the same is this:

    if(variableOfTypeT is Base)
    

    But that has a different meaning.
    The first statement (with typeof(Base)) only is true if T is Base. It will be false for any type derived from Base.
    The second statement (variableOfTypeT is Base) is always true in your class, because any class derived from Base will return true for a check for its base class.

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

Sidebar

Related Questions

Hope my question is correct to the post. <form action=process.php?id=<?php echo intval($order['id']);?> method=post> <ul>
This is my first question here, so I hope everything is correct. I've hacked
a question that I hope does not sound crazy: On a multilingual site, imagine
Let's take a concrete example and hope I can be clear. Suppose the (ordered)
Hope this question is not stupid since I am an amateur web designer. I
I hope this question isn't too general. Well, the situation is that I am
I hope this question isn't too dumb. We have many websites internally that need
I hope this question is not 'controversial' - I'm just basically asking - has
I hope this question is not a duplicate but I didn't find anything equivalent
I hope this question isn't too open ended, but a nudge in the right

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.