HomeSearch

C# typeof and nameof Operators

This C# article covers the typeof and nameof operators. The example code gets the type data of objects.

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.

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[]

Typeof notes.

The above program shows the string representations of the Type pointers that are returned when the typeof(T) expressions are evaluated.

Metadata: The metadata in .NET is a relational database that contains many tables for types in C# programs.

ToString: This program calls ToString on the Type pointers. This returns a string representation of the type, such as "System.Char".

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

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

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

Typeof performance.

Typeof uses a simple and fast form of reflection on the type pointer. Sadly, 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. You can use this static Type field wherever you require the Type pointer.

Warning: The .NET Framework has optimizations to compare 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(); } Output Iterations: 1000000000 Typeof expression used: 2.55 ns Type object cached: 0.64 ns

Performance results.

This experiment shows that evaluating the typeof expression on a class in a tight loop required 2.55 nanoseconds each iteration.Benchmark

Static: Copying a cached static Type pointer only required 0.64 nanoseconds. A static field saved almost 2 nanoseconds.

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.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.