Home
Map
typeof and nameof OperatorsUse the typeof and nameof operators to get the type data of objects. Evaluate typeof performance.
C#
This page was last reviewed on May 11, 2023.
Typeof. The C# typeof operator returns Type objects. It is often used as a parameter or as a variable or field. It is part of an expression that acquires the Type pointer.
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.
Type
Reflection
sizeof
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.
Info The typeof operator uses reflection to access the metadata descriptions of the types.
Here We display the string representation of these Type pointers. We assign the result of the typeof operator to a Type variable or field.
Console.WriteLine
Tip This program calls ToString on the Type pointers—this returns a string representation of the type.
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 } }
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).
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); } }
size 100 animal
Benchmark, typeof. Typeof uses a simple form of reflection on the type pointer. Some forms of reflection can 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 On .NET 5 for Linux (in 2021) the 2 versions of the code are about the same speed.
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")); } }
0.27 ns typeof expression 0.27 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.GetName
Enum.Parse
DataTable
Convert 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.
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 .NET.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 11, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.