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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:57:36+00:00 2026-05-25T14:57:36+00:00

I use the is operator to find a certain class: for(int i=0; i<screens.Count; i++){

  • 0

I use the “is” operator to find a certain class:

for(int i=0; i<screens.Count; i++){
  if(screen is ScreenBase){
    //do something...
  }
}

This works fine especially as it finds any class that inherits from the ScreenBase but not the base classes from ScreenBase.

I would like to do the same when I know only the Type and don’t want to instantiate the class:

Type screenType = GetType(line);
if (screenType is ScreenBase)

But this comparsion produces a warning as it will compare to the “Type” class.

The only alternative I know would be to compare with ==typeof but this would test only for the exact type and not the inherited ones.
Is there a way to get a similar behaviour like the “is” operator but for the type described by the Type-class?

  • 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-25T14:57:36+00:00Added an answer on May 25, 2026 at 2:57 pm

    If you want to know specifically if it derives from the type, use Type.IsSubclassOf(). This will not work for interfaces.

    Type screenType = GetType(line);
    if (screenType.IsSubclassOf(typeof(ScreenBase)))
    {
        // do stuff...
    }
    

    Otherwise if you want to know if the type could be assigned to a variable of a certain type, use Type.IsAssignableFrom(). This will work for interfaces.

    Type screenType = GetType(line);
    if (typeof(ScreenBase).IsAssignableFrom(screenType)) // note the usage is reversed
    {
        // do stuff...
    }
    

    Do note that you don’t necessarily need a type object to determine this, you can do this with an instance of the object using Type.IsInstanceOfType(). It will behave more or less like IsAssignableFrom().

    if (typeof(ScreenBase).IsInstanceOfType(line)) // note the usage is reversed
    {
        // do stuff...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use operator() as a subscript operator this way: double CVector::operator() (int i) const
When must we use this operator by events? What is its usage?
I have this very simple wrapper template: template<class T> struct wrapper { inline operator
How do I find a literal % with the LIKE-operator? #!/usr/bin/perl use warnings; use
I use the ternary operator quite often but I've not been able to find
I'm trying to use operator overloading to define the basic operations (+,-,*,/) for my
When must we use checked operator in C#? Is it only suitable for exception
I know that we cant use assignment operator in if statements in java as
While using the vector why do we sometime use the operator[] like homework[mid] but
I want to derive a stringstream so that I can use the operator<< to

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.