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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:57:11+00:00 2026-05-15T22:57:11+00:00

As we know, C# objects have a pointer to their type, so when you

  • 0

As we know, C# objects have a pointer to their type, so when you call GetType() it checks that pointer and returns the real type of an object. But if I do this:

A objA = new A();
object obj = (object)objA;
if (obj.GetType() == typeof(object)) ; // this is true

But what happens here object obj = (object)objA;? Does it create some sort of reference object, that references objA, but has a type pointer to object, or is it a completely new object, that just happens to be pointing to the same properties, fields, etc. as objA? Certainly, you can access both objects now and they will have a different type, but point to the same data. How does that work?

The other question is: is GetType() guaranteed to return the actual type of an object? For instance, say there is a method with signature void Method(object sender) and we pass object of type A as a parameter. Will sender.GetType() return type A, or object? And Why?

Other tricky thing is that you can do (A)obj and it will work. How does CLR now that obj was once of type A?

Would be glad if someone could break it down a bit clearer than “C# via CLR” does.

Update. My bad, should have run the code before posting the question. So, if GetType() really always returns the real type, than all other questions become clear too.

  • 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-15T22:57:11+00:00Added an answer on May 15, 2026 at 10:57 pm

    As we know, C# objects have a pointer to their type, so when you call GetType() it checks that pointer and returns the real type of an object.

    Correct.

    if I do this:

    class A {}
    class P
    {
        public static void Main()
        {
            A objA = new A(); 
            object obj = (object)objA; 
            bool b = obj.GetType() == typeof(object) ; // this is true
        }
    }
    

    No, that’s false. Try it!

    But what happens here object obj = (object)objA;?

    The reference that is in objA is copied to the variable obj. (Unless A is a value type, in which case it is boxed and a reference to the box is copied to obj.)

    Does it create some sort of reference object, that references objA, but has a type pointer to object, or is it a completely new object, that just happens to be pointing to the same properties, fields, etc. as objA?

    Neither. It copies the reference, period. It is completely unchanged.

    Certainly, you can access both objects now and they will have a different type, but point to the same data. How does that work?

    It doesn’t. The question is predicated on an assumption which is false. They will not have different types. They are the same reference. The variables have different types, but that’s irrelevant; you’re not asking the variable for its type, you’re asking the contents of the variable for its type.

    is GetType() guaranteed to return the actual type of an object?

    For your purposes, yes. There are obscure situations involving COM interop where it does not.

    For instance, say there is a method with signature void Method(object sender) and we pass object of type A as a parameter. Will sender.GetType() return type A, or object?

    Type A.

    And Why?

    Because that’s the type of the object.

    Other tricky thing is that you can do (A)obj and it will work. How does CLR now that obj was once of type A?

    The C# compiler generates a castclass instruction. The castclass instruction does a runtime check to verify that the object reference implements the desired type. If it does not then the CLR throws an exception.

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

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer http://www.codedread.com/svg-support.php May 16, 2026 at 5:00 pm
  • Editorial Team
    Editorial Team added an answer Without seeing any code I would say that you haven't… May 16, 2026 at 5:00 pm
  • Editorial Team
    Editorial Team added an answer For most Unix-style C programs, the incantation is: ./configure make… May 16, 2026 at 4:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have objects which create other child objects within their constructors, passing 'this' so
I have a pointer to a COM object that implements an undocumented interface. I
I know this question must have been covered endless of times, but I've searched
Let's say I'm creating an OpenGL game in C++ that will have many objects
I have recently tried sharpening my rails skills with this tool: http://github.com/edgecase/ruby_koans but I
What is the best approach to encapsulate objects and manage their lifetime? Example: I
I am working on some software that should be used for a special type
I'm planning a system that combines various data sources and lets users do simple
Let's say you have a class class C { int * i; public: C(int
In my Java project, I have a vector of various types of Traders. These

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.