C# typeof and nameof Operators

Use the typeof and nameof operators to get the type data of objects. Evaluate typeof performance.
Typeof returns Type objects. 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.Type
Nameof, meanwhile, returns a string with a variable's name. It works at compile-time. It is a special compiler feature that simplifies some programs.
Typeof example. Here we use typeof in a simple program. The types the program uses are found in the System namespace and the System.IO namespace.

Metadata: The typeof operator uses reflection to access the metadata descriptions of the types.

Type: We display the string representation of these Type pointers. We assign the result of the typeof operator to a Type variable or field.


ToString: This program calls ToString on the Type pointers—this returns a string representation of the type.

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[]
Nameof example. The nameof keyword returns a string containing the identifier of a variable. This is a compile-time feature. The compiler knows the names of variables, and it inserts them.

So: We can use nameof even on local variables. We use it here to get an argument's name (size) and a local's name (animal).

C# program that uses nameof using System; class Program { static void Test(int size) { // ... Write argument name with nameof. Console.WriteLine(nameof(size)); Console.WriteLine(size); // ... Use nameof on a local variable. var animal = "cat"; Console.WriteLine(nameof(animal)); } static void Main() { // Call method. Test(100); } } Output size 100 animal
Benchmark, Typeof. Typeof uses a simple (and fast) form of reflection on the type pointer. Reflection usually will slow down a program. In critical loops, this may be a problem.Benchmark

Version 1: This code uses the typeof operator. If this returns null, the program is exited.

Version 2: This version of the code uses a cached static Type pointer. The same if-check is done in this loop as in version 1.

Result: Evaluating typeof on a class in a tight loop required more time than a static Type pointer.

Info: The .NET Framework has optimizations to compare multiple typeof expressions. This optimization is not always useful.

C# program that optimizes typeof using System; using System.Diagnostics; class Program { static Type _type = typeof(Stopwatch); const int _max = 100000000; static void Main() { var s1 = Stopwatch.StartNew(); // Version 1: use typeof. for (int i = 0; i < _max; i++) { Type type = typeof(Stopwatch); if (type == null) { throw new Exception(); } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use static type cache. for (int i = 0; i < _max; i++) { Type type = _type; if (type == null) { throw new Exception(); } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } } Output 1.64 ns typeof expression 1.37 ns static type
Typeof uses. Some common uses of the typeof operator are the Enum static methods, the DataTable class and similar classes, and the ArrayList conversion methods.Enum.GetNameEnum.ParseDataTableConvert ArrayList, Array
Notes, metadata. Type pointers are returned when the typeof(T) expressions are evaluated. The metadata in .NET is a relational database that contains many tables for types in C# programs.

Tip: The lowercase types such as "char" and "int" are aliased to the framework types "System.Char" and "System.Int32" in mscorlib.dll.

Using System
A summary. Typeof is an operator. We assigned the result of typeof expressions to a Type field or variable. There are many common uses of the typeof operator in the .NET Framework.
With nameof, we access a string that indicates a variable's name. Instead of a Type, it returns a string name. This is also helpful in many programs.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to