C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It is often used as a parameter or as a variable or field. The typeof operator is part of an expression that acquires the Type pointer for a class or value type.
Example. We first look at a program written that acquires the Type pointer for various types available in the compilation unit. The types the program uses are found in the System namespace and the System.IO namespace.
The typeof operator uses reflection to access the metadata descriptions of the types. The program then displays the string representation of these Type pointers. We assign the result of the typeof operator to a Type variable or field.
C# program that uses typeof expressions using System; using System.IO; class Program { static Type _type = typeof(char); // Store Type as field static void Main() { Console.WriteLine(_type); // Value type pointer Console.WriteLine(typeof(int)); // Value type Console.WriteLine(typeof(byte)); // Value type Console.WriteLine(typeof(Stream)); // Class type Console.WriteLine(typeof(TextWriter)); // Class type Console.WriteLine(typeof(Array)); // Class type Console.WriteLine(typeof(int[])); // Array reference type } } Output System.Char System.Int32 System.Byte System.IO.Stream System.IO.TextWriter System.Array System.Int32[]
This program shows the string representation of the Type pointer that is returned when the typeof(T) expression is evaluated. Whenever the typeof operator is used, the metadata is used and the Type reference is returned.
Tip: The metadata in .NET is a relational database that contains many tables for types in your C# programs.
And: The metadata is organized into tokens that relate to offsets in related tables.
Next, this program actually calls the ToString method defined on the Type pointers in memory. The default ToString method returns a string representation of the type, such as "System.Char".
Note: The lowercase types such as "char" and "int" are aliased to the framework types "System.Char" and "System.Int32" in mscorlib.dll.
Uses. Some common uses of the typeof operator are the Enum static methods, the DataTable class and similar classes, the ArrayList conversion methods, and the HttpWorkerRequest methods used in ASP.NET. We provide further descriptions.
Enum.GetNameEnum.Parse Tutorial: TryParse, IsDefinedDataTable ExamplesConvert ArrayList to ArrayHttpWorkerRequest Example
Performance. The typeof operator uses a simple and fast form of reflection on the type pointer. Unfortunately, any kind of reflection is usually too slow for optimized code in a critical loop. But you can store the results of the typeof expression.
Then: You can use this static Type field wherever you require the Type pointer. This changes a reflection call to a field load.
This is most useful in method calls that require a Type object. Please note that the .NET Framework has special optimizations when comparing multiple typeof expressions. This optimization is not useful in that case.
Static type object used: C# static Type _type = typeof(Stopwatch); Loop statements with typeof expression: C# Type type = typeof(Stopwatch); if (type == null) { throw new Exception(); } Loop statements with Type cached: C# Type type = _type; if (type == null) { throw new Exception(); } Results of the benchmark Iterations: 1000000000 Typeof expression used: 2.55 ns Type object cached: 0.64 ns
This experiment shows that evaluating the typeof expression on a class in a tight loop required 2.55 nanoseconds each iteration. Copying a cached static Type pointer only required 0.64 nanoseconds.
We found that using a static Type field can save almost 2 nanoseconds each time it is used. This is only helpful for typeof expressions that are evaluated extremely frequently. The additional complexity is a negative.
Summary. We looked at the typeof operator. We assigned the result of typeof expressions to a Type field or variable. Next we discussed the metadata organization and then looked at common uses of the typeof operator in the .NET Framework.
Finally: We tested the typeof expression against a static Type cached pointer. This is a viable micro-optimization.