Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. difference between GetType() method , is operator and typeof operator

difference between GetType() method , is operator and typeof operator

Scheduled Pinned Locked Moved C#
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RuchirD
    wrote on last edited by
    #1

    Can anyone tell me difference between GetType() method, is operator and TypeOf() operator I'll be very much thankful to you. Regards. Ruchir. RuchirDhar Dwivedi Software Engineer Windowmaker Software Pvt.Ltd. Baroda, India.

    S 1 Reply Last reply
    0
    • R RuchirD

      Can anyone tell me difference between GetType() method, is operator and TypeOf() operator I'll be very much thankful to you. Regards. Ruchir. RuchirDhar Dwivedi Software Engineer Windowmaker Software Pvt.Ltd. Baroda, India.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      The typeof operator returns an instance of System.Type corresponding to the class name you passed it to. You can't determine the type of an object using the typeof operator. For example

      class Foo{}
      class Test
      {
      public static void Main(string []args)
      {
      Console.WriteLine(typeof(Foo)); //Prints Foo
      Foo f = new Foo();
      Console.WriteLine(typeof(Foo)); //Does NOT work.
      }
      }

      The GetType function gets you the runtime type of an object. Note that it is the *runtime* type and not the static type. For eg.

      public void SomeFunc()
      {
      object obj = new Foo();
      Console.WriteLine(obj.GetType());
      }

      will print Foo and not Object. The is operator checks the runtime type of the arguments and sees if the LHS is (or can be cast to) the RHS. It returns true if the casting can succeed and false otherwise. For eg

      public void SomeFunc()
      {
      Foo f = new Foo();
      Console.WriteLine(f is Foo); //Prints true.
      object obj = new Foo();
      Console.WriteLine(obj is Foo); //Prints true
      object obj2 = new object();
      Console.WriteLine(obj2 is Foo); //Prints false
      }

      Hope this helps. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups