In C#, we use the typeof()
method to get the name of a class in C#. This method is similar to the GetType()
method, which helps determine the type of variable. We use the typeof()
method with some properties such as Name
, FullName
, and Namespace
. The Name
property returns the name of the class, FullName
returns the name of the class along with its namespace, and Namespace
returns the namespace only.
typeof(class)// ORtypeof(class).property
class
: We want to determine the name of this class.
property
: We use any of the properties listed above.
The value returned is a string denoting the name of a particular class.
using System;using System.Text;namespace Test{class Program{static void Main(string[] args){// for intConsole.WriteLine("Class int...");Console.WriteLine("Name: " + typeof(int).Name);Console.WriteLine("FullName: " + typeof(int).FullName);Console.WriteLine("Namespace: " + typeof(int).Namespace);Console.WriteLine();// for charConsole.WriteLine("Class char...");Console.WriteLine("Name: " + typeof(char).Name);Console.WriteLine("FullName: " + typeof(char).FullName);Console.WriteLine("Namespace: " + typeof(char).Namespace);Console.WriteLine();// for boolConsole.WriteLine("Class bool...");Console.WriteLine("Name: " + typeof(bool).Name);Console.WriteLine("FullName: " + typeof(bool).FullName);Console.WriteLine("Namespace: " + typeof(bool).Namespace);Console.WriteLine();// for doubleConsole.WriteLine("Class double...");Console.WriteLine("Name: " + typeof(double).Name);Console.WriteLine("FullName: " + typeof(double).FullName);Console.WriteLine("Namespace: " + typeof(double).Namespace);Console.WriteLine();}}}
int
its Fullname, and namespace.char
, its Fullname, and namespace. We'll repeat it for classes bool
and double
.