10.3 typeof operator


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 

Output:

 c:\expt>test System.Int32 System.Decimal System.String System.Object Mok.Book.Test 

int and decimal are special simple value types which correspond to the .NET System.Int32 and System.Decimal classes respectively. string and object are C# aliases for the System.String and System.Object classes too.

One important point to note is that the typeof operator can be applied only on type names “ it cannot take in an object. For example, the following code fragment will result in a compilation error:

 // Test is a user defined class Test tObject = new Test(); Type t1 = typeof(tObject); // compilation error 

The compiler will attempt to search for the type definition of t , and on not finding it will throw an exception which says:

[View full width]
 
[View full width]
The type or namespace name 'tObject' could not be found (are you missing a using directive graphics/ccc.gif or an assembly reference?)

Here is another example to illustrate how typeof can also work with interfaces and structs (see Chapter 26 for structs).

 1: using System;  2:  3: interface IMyInterface {}  4: struct MyStruct {}  5:  6: namespace MyNamespace{  7:   class ClassA:IMyInterface {}  8: }  9: 10: class ClassB{ 11:   public static void Main(){ 12:     Type a = typeof(IMyInterface); 13:     Type b = typeof(MyStruct); 14:     Type c = typeof(MyNamespace.ClassA); 15:     Type d = typeof(ClassB); 16: 17:     Console.WriteLine(a); 18:     Console.WriteLine(b); 19:     Console.WriteLine(c); 20:     Console.WriteLine(d); 21:   } 22: } 

Output:

 c:\expt>test IMyInterface MyStruct MyNamespace.ClassA ClassB 

Getting a Type object of a class or interface will be useful when you are writing codes which make use of reflection. The Type class has several useful methods which enable you to find out more about a particular type dynamically.

10.3.1 Comparing Object.GetType() , Type.GetType() , and typeof

You might have noticed that System.Object (which is the superclass for all classes in C#) has a GetType() method which also returns a Type object. Study the program below.

 1: using System; 2: class Test{ 3:   public static void Main(){ 4:     Test tObject = new Test(); 5:  Type t  =  tObject.GetType();  6:     Console.WriteLine(t); 7:   } 8: } 

Output:

 c:\expt>test Test 

So what's the difference between using GetType() and the typeof operator? Notice that GetType() works on an object, while the typeof operator takes in a type name as the operand.

Replacing line 5 with

 5:     Type t = typeof(tObject); 

will result in a compilation error because tObject is not a type name, but a variable.

The System.Type class also has a static GetType() method which takes in a type name (a string) as a method parameter. Assuming MyClass is a user-defined class, this statement is valid:

 Type t = Type.GetType("MyClass"); 

The above statement is functionally identical to:

 Type t = typeof(MyClass); 

There are two main differences between Type.GetType() and typeof :

  • typeof cannot take in a variable as an operand (even a constant), while you can pass in string variables into Type.GetType() ;

  • typeof is evaluated during compile time and hence is faster during execution, while Type.GetType() is evaluated during runtime “ this also explains why typeof cannot operate on a variable.

Table 10.3 summarizes the differences.

Table 10.3. Comparing the typeof operator with Type.GetType() and Object.GetType() “ all of them return a System.Type object
 

typeof operator

Object.GetType()

Type.GetType()

Parameter

Type name

Object name

Type name (as a string)

Execution

Compile time

Runtime

Runtime

Example

typeof(MyClass)

myClass.GetType()

Type.GetType("MyClass")



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net