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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:52:01+00:00 2026-05-17T01:52:01+00:00

Cannot explain what is going on the following program. GetType is returning the type

  • 0

Cannot explain what is going on the following program. GetType is returning the type I want to return and not the original one. Does that mean we cannot rely on GetType? is operator is right though. Can anybody please explain it in detail?

using System;

namespace ConsoleApplication2
{
    public class MyClass
    {
        public Type GetType()
        {
            return typeof(Program);
        }

    }

class Program
{
    static void Main(string[] args)
    {
        MyClass mc = new MyClass();

        if (mc.GetType() == typeof(Program))
        {
            Console.WriteLine("Confused."); 
        }
        if(mc is Program)
        {
            Console.WriteLine(mc.GetType()); // Don't get inside the if. Why?
        }

    }
}
}

Update: I am reading the book CLR via C# 3rd edition. In chapter 4 (2nd page) when it explains different methods in System.Object it says

“The GetType method is nonvirtual,
which prevents a class overriding this
method and lying about its type”

While I agree about the first statement, I am lying about MyClass type. ain’t I?

  • 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-17T01:52:02+00:00Added an answer on May 17, 2026 at 1:52 am

    is operator implemented in terms of as operator and finally use isinst IL instruction. And of course this instruction don’t know about your not virtual GetType method that you define in some class in your inheritance hierarchy.

    To understand this “confusing” behavior lets “implement” our own version of the “is operator”:

    public class MyClass
    {
        public Type GetType()
        {
            return typeof(Program);
        }
    
    }
    
    class Program {
    
        //this is oversimplified implementation,
        //but I want to show the main differences
        public static bool IsInstOf(object o, Type t)
        {
            //calling GetType on System.Object
            return o.GetType().IsAssignableFrom(t);
        }
    
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
    
            //calling MyClass non-virtual version for GetType method
            if (mc.GetType() == typeof(Program))
            {
                //Yep, this condition is true
                Console.WriteLine("Not surprised!");
            }
    
            //Calling System.Object non-virtual version for GetType method
            if (IsInstOf(mc, typeof(Program)))
            {
                //Nope, this condition isn't met!
                //because mc.GetType() != ((object)mc).GetType()!
            }
    
            Console.ReadLine();
        }            
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain why I'm getting this error Type mismatch: cannot convert from
In this commit there is a change I cannot explain deferred.done.apply( deferred, arguments ).fail.apply(
Can someone explain to me why I cannot use document.getElementById('id').value inside of a function?
Cannot dump Stack Overflow XML file into SQL Server 2008. Could you please explain
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
I would like someone to explain me what _forward is exactly doing, I cannot
I have read that a subclass cannot inherit private fields or methods. However, in
I'm going to try to explain this in simple terms, because it's probably shorter
I found the following code in SO. Does this really work? String xml =
I'm following the cs193p course on iTunes U, and all is going well until

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.