datatype of null RRS feed

  • Question

  • User1667461426 posted

    How GetType()  is implemented in Object Class?how it accepts parameters and how it return the type of any object?similarly for typeOf? I want To implement typeOf() and GetType().

    Tuesday, March 3, 2015 5:05 AM


All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2015 5:22 AM
  • User753101303 posted


    Not sure what you meant. You doesn't have to "implement" it. typeof is a C# keyword that just works and GetType is inherited from System.Object and does the right thing.

    Tuesday, March 3, 2015 6:00 AM
  • User-434868552 posted

    @Sowmya Manch...   welcome to forums.asp.net

    This article should help you pose questions to any forum:


    "Clarity is important, both in question and in answer."

    https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx "Object Class"

    https://msdn.microsoft.com/en-us/library/system.object.gettype(v=vs.110).aspx "Object.GetType"

    MSDN is one of your best friends; study it!


    http://blogs.msdn.com/b/dotnet/archive/2014/02/24/a-new-look-for-net-reference-source.aspx "Browsing .NET Reference Source"

    Read the above article and watch the video.


       excerpts from Object:

    ** Class:  Object
    ** Object is the root class for all CLR objects.  This class
    ** defines only the basics.
    namespace System {
        using System;
        using System.Runtime;
        using System.Runtime.InteropServices;
        using System.Runtime.CompilerServices;
        using System.Runtime.ConstrainedExecution;
        using System.Runtime.Versioning;
        using System.Diagnostics.Contracts;
        using CultureInfo = System.Globalization.CultureInfo;
        using FieldInfo = System.Reflection.FieldInfo;
        using BindingFlags = System.Reflection.BindingFlags;
    // The Object is the root class for all object in the CLR System. Object 
    // is the super class for all other CLR objects and provide a set of methods and low level
    // services to subclasses.  These services include object synchronization and support for clone
    // operations.
    //This class contains no data and does not need to be serializable 
    public class Object
    // Returns a Type object which represent this object instance.
    [System.Security.SecuritySafeCritical]  // auto-generated
    public extern Type GetType();

    one use of .GetType is when one is not certain of the var type on the left side of a LINQ query; once i get that type from .GetType, i  replace var with the actual type.

    .GetType is also useful with this example from https://msdn.microsoft.com/en-us/library/system.object.gettype(v=vs.110).aspx:

    using System;
    public class MyBaseClass {}
    public class MyDerivedClass: MyBaseClass {}
    public class Test 
       public static void Main() 
          MyBaseClass myBase = new MyBaseClass();
          MyDerivedClass myDerived = new MyDerivedClass();
          object o = myDerived;
          MyBaseClass b = myDerived;
          Console.WriteLine("mybase: Type is {0}", myBase.GetType());
          Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
          Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
          Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
    // The example displays the following output: 
    //    mybase: Type is MyBaseClass 
    //    myDerived: Type is MyDerivedClass 
    //    object o = myDerived: Type is MyDerivedClass 
    //    MyBaseClass b = myDerived: Type is MyDerivedClass 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2015 8:55 AM
  • User-271186128 posted

    Hi Sowmya,

    The Object.GetType method will gets the Type of the current instance. And the typeof keyword is used to obtain the System.Type object for a type.

    Here are some articles about them, please refer to them.

    Object.GetType Method: https://msdn.microsoft.com/en-us/library/system.object.gettype(v=vs.110).aspx 

    Type.GetType Method: https://msdn.microsoft.com/en-us/library/s2wzt390(v=vs.110).aspx

    typeof (C# Reference): https://msdn.microsoft.com/en-us/library/58918ffs.aspx

    Best Regards,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 4, 2015 12:58 AM