Home
C#
Type Class
Updated Jul 18, 2021
Dot Net Perls
Type. This class describes data types. It stores type information in a variable, property or field. The Type class represents the program's metadata (its structure).
Reflection
Background info. First, the Type class is found in the System namespace. You can use Type variables in the same way as other variables such as string or int.
Example. Here we see you can assign Type variables to the result of a typeof expression. And methods receive a Type parameter as an argument.
Here A Type is returned by the typeof operator and the GetType method. We call method on the Type.
typeof
GetType
Detail This shows that you can use Type variables and assign them to the result of evaluations of the type of operator.
Tip The Type variables are loaded onto the evaluation stack in the same way as other variables are.
using System; class Program { static void Main() { // Use type variables. // ... Then pass the variables as an argument. Type type1 = typeof(string[]); Type type2 = "string".GetType(); Type type3 = typeof(Type); Test(type1); Test(type2); Test(type3); } static void Test(Type type) { // Print some properties of the Type formal parameter. Console.WriteLine("IsArray: {0}", type.IsArray); Console.WriteLine("Name: {0}", type.Name); Console.WriteLine("IsSealed: {0}", type.IsSealed); Console.WriteLine("BaseType.Name: {0}", type.BaseType.Name); Console.WriteLine(); } }
IsArray: True Name: String[] IsSealed: True BaseType.Name: Array IsArray: False Name: String IsSealed: True BaseType.Name: Object IsArray: False Name: Type IsSealed: False BaseType.Name: MemberInfo
Type notes. In the C# language, the Type class is not used in the inheritance and class-based system. Instead, classes inherit from the Object type.
Type usage. The "Type" type is a representation of the metadata, which is data about the program. It can be seen as a sort of information about the program.
A summary. Type objects can be stored as fields and static fields. They can be passed as arguments and used as local variables. Some functions in C# receive Type arguments.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jul 18, 2021 (edit).
Home
Changes
© 2007-2025 Sam Allen