O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.3. typeof operator

The typeof operator is used to get a representative System.Type object of a type or object. There is only one System.Type object for each type. C#'s typeof is very similar to java.lang.Object's getClass() method. getClass() returns a java.lang.Class object that represents the runtime type of a Java object.

Study this example.

 1: using System; 2: namespace Mok.Book{ 3: class Test{ 4: public static void Main(){ 5: Type t1 = typeof(int); 6: Type t2 = typeof(decimal); 7: Type t3 = typeof(string); 8: Type t4 = typeof(object); 9: Type t5 = typeof(Test); 10: 11: Console.WriteLine(t1); 12: Console.WriteLine(t2); 13: Console.WriteLine(t3); 14: Console.WriteLine(t4); 15: Console.WriteLine(t5); 16: } 17: } 18: } // end namespace  ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required