TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Type Class

This C# article covers Type from the System namespace. It provides example code.

Type describes data types.

It stores type information in a variable, property or field. The Type class represents the program's metadata, which is a description of its structure but not the instructions that are executed.

Metadata

Example. First, the Type class is found in the System namespace in the .NET Framework base class library. You can use Type variables in the same way as other variables such as string or int.

Int

Note: You can assign Type variables to the result of a typeof expression. And methods receive a Type parameter as an argument.

C# program that uses Type variables

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();
    }
}

Output

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

The program shows that you can use Type variables and assign them to the result of evaluations of the typeof operator. The Type variables are loaded onto the evaluation stack in the same way as other variables are.

Also, the Test method receives a formal parameter of "Type" type. You can pass it typeof expressions, or any expression that evaluates to a Type pointer. The program also shows some properties of the Type class being accessed.

Typeof Operator

Metadata. In the C# language, the Type class is not actually used in the inheritance and class-based system. Instead, classes inherit from the Object type. The "Type" type is a representation of the metadata, which is data about the program.

Therefore: The Type class can be seen as a sort of information about the program, but not the program itself.

Summary. Type objects can be stored as fields and static fields. They can be passed as arguments and used as local variables. Type data is stored on the managed heap because it is an object reference. And some functions receive Type arguments.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf