0

Is the Type object a reference type?
Let's say I have this code:

String s = "AAA";
Type b = s.GetType();

Does b point at a type object located on heap?

How can we create a TYPE object for an abstract class?
Again this code:

String s = "AAA";
Type b = s.GetType();

How can s.GetType(); create a TYPE object if this is an abstract class? You can't create instance of abstract class.

So Object.GetType() method, return a type derived from System.Type, namely System.RuntimeType - This i understand. BUT which object returns by typeof(object)? It should be a type that also derived from System.Type, since type class itself is abstract. what the name of this type?

  • 1
    a type of an object is not an instance of that object's class. therefore the abstract modifier does not apply to querying the type, but the fact that a class is abstract will end up as a property value of its type (`var abstracttype = typeof(abstract_class);` then `abstracttype.IsAbstract` will be true). – Cee McSharpface Nov 06 '18 at 12:07
  • 1
    System.Type is a class, so yes, it is a reference type. – Zohar Peled Nov 06 '18 at 12:07
  • Why do you want to know this? What problem are you trying to solve? – CodeCaster Nov 06 '18 at 15:24

3 Answers3

0

The non-virtual method Object.GetType() is implemented by the .NET runtime. It returns the type info for the object it is called on.

The actual implementation of the returned Type is an implementation detail, but the .NET runtime will return a type derived from System.Type, namely System.RuntimeType. See also What's the difference between System.Type and System.RuntimeType in C#?.

Regarding your edit:

which object returns by typeof(object)?

typeof(T) gets compiled to Type.GetTypeFromHandle(), and which Type-derived type that method returns is an implementation detail again.

I just want to know how is it possible to create a type object if type object is an abstract class?

You can't. The mscorlib library contains public abstract class System.Type, which public abstract class System.TypeInfo inherits, which in turn is inherited by internal class RuntimeType. So internally, the CLR does something like this:

public Type GetType()
{
    var typeInfo = new RuntimeType();
    // set some properties
    return typeInfo();
}

And because RuntimeType inherits from Type, an instance of that type can be returned from that method.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • So Object.GetType() method, return a type derived from System.Type, namely System.RuntimeType - This i understand. BUT which object returns by typeof(object)? It should be s type that also derived from System.Type, since type class is abstract. what the name of this type? – Amichai Simcha Nov 06 '18 at 14:41
  • hi thank you for your reply. Can you please explain this again: and which Type-derived type that method returns is an implementation detail again. – Amichai Simcha Nov 06 '18 at 15:30
  • An implementation detail means: you'll have to do with what the documentation states; how the runtime implements it exactly is subject to change. So like I asked under your question: what exactly do you want to know, and why? What are you going to do with the answers? – CodeCaster Nov 06 '18 at 15:33
  • I just want to know how is it possible to create a type object if type object is an abstract class? So i understand that the type return by "gettype()" method is a derived class called "runtime type" (Runtime type) derived from the abstract class. The same i want to know about typeof() method, in this case which type class derived from type abstract class? – Amichai Simcha Nov 06 '18 at 15:39
  • You can't. That's why the runtime returns a `System.RuntimeType`, which inherits from `System.Type`. You can use an abstract type as the return type for a method. – CodeCaster Nov 06 '18 at 15:40
0

Does b point at a type object located on heap?

Yes, since System.Type is a reference type it is always allocated on the heap.

How can we create a TYPE object for an abstract class?

var t = typeof(MyAbstractClass);

Onur Gumus
  • 1,389
  • 11
  • 27
-1

You could easily get information about this on the official documentation. Please refer the docs

Type does not point to the actual objects. It just gets the information on the Type of the object.

As you cannot create an instance of an abstract class, you would not be able to invoke the GetType on that class.

Shahzad
  • 1,677
  • 1
  • 12
  • 25