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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:44:45+00:00 2026-05-22T16:44:45+00:00

I’m a little foggy on System.Type versus an actual class type (like Object or

  • 0

I’m a little foggy on System.Type versus an actual class type (like Object or XmlDocument) in .NET… will this code correctly determine if the type of a particular object is equal to a class I specify?

' Given "myObject" (unknown type), and some class type (let's say "MyClass")...

If myObject.GetType.Equals(MyClass)

If TypeOf(myObject) Is MyClass

If myObject.GetType() Is MyClass

Which one is correct?

Bonus points if you can provide some information on what a class identifier is versus what a System.Type is. 🙂

Note: The language doesn’t matter here, VB.NET or C# is fine, the code above is pseudocode.

  • 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-22T16:44:45+00:00Added an answer on May 22, 2026 at 4:44 pm

    Fist let’s take a look on the three options you gave:

    If myObject.GetType.Equals(MyClass)
    

    This will probably result in a error, since the equals expects a System.Type, not a class. A class definition is not a System.Type, but you can retrieve it using the typeof operator. So you could do instance.Equals(typeof(MyClass)), which would return true if the object is of the given class.

    If TypeOf(myObject) Is MyClass
    

    Conversely, you can’t use typeof with instances, only with classes, so the above code would fail. Also, the is operator automatically checks the typing so you can’t do a typeof or a GetType when using it. You should go with if myObject is MyClass, which would return true if myObject can be cast to MyClass. This is different from saying that it’s an instance of that type, because it could be that myObject is an instance of a class that inherits from MyClass.

    If myObject.GetType() Is MyClass
    

    Again, the is operator already checks the type on both operands, so you should go with if myObject is MyClass.


    All that said, I’d like to explain the “theory” behind the type system. I’m no specialist, so I’m giving you a more practical explanation:

    • A class definition label (like MyClass) is not a System.Type. A System.Type is a metadata class that is generated by the CLR to represent the type defined by your label. To retrieve the System.Type related to a certain class definition label, use the typeof operator as follows:

      System.Type MyClassType = typeof(MyClass);
      
    • On a object instance, you can retrieve the System.Type metadata by calling the method GetType() on it. It will give you an instance of System.Type related to the class that represents the actual instance. This means that if your object is being treated by the compiler as a interface or a base class, .GetType() still gives you the most derived type for that instance.

    • You can compare System.Type in order to check if two objects are instances of the same class, but again, beware that your instance can be of a more derived type; The equality will fail (the System.Typeof a more derived class is different than that of a less derived one).

    • If you need to take inheritance into account, you can use the method IsAssignableFrom, like this:

      BaseClass instance = new DerivedClass();
      
      System.Type type = instance.GetType();
      
      if ((typeof(BaseClass)).IsAssignableFrom(type))    // returns true
      {
      }
      
    • C# and VB.Net gives you two operators that enables you to do type checking on the fly, is and as. is does automatic typing retrieval and is preferred over getting the System.Type yourself. It accounts for inheritance as well:

      DerivedClass instance = new DerivedClass();
      
      System.Type type = instance.GetType();
      
      if (instance is BaseClass)    // returns true
      {
      }
      
    • If you need to check the type and cast the object use as:

      DerivedClassinstance = new DerivedClass();
      
      System.Type type = instance.GetType();
      
      AnotherClass another = instance as AnotherClass;
      
      if (another == null)    // returns true
      {
          // Do proper error treatment... throw an exception or something
      }
      

      What you cannot do with as is not perform proper result checking; The problem is that if you don’t check it for null and use it, you get an NullReferenceException, which will hide the correct problem (the cast failed). If you are sure you can do the cast, then use a explicit cast:

      DerivedClassinstance = new DerivedClass();
      
      System.Type type = instance.GetType();
      
      AnotherClass another = (AnotherClass)instance; // throws
      

      This will throw an InvalidCastException, so the code will be easier to debug.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

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.