C# Object Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, you can get the current class name using the GetType
method, and you can create an instance of a class from its string class name using the Activator.CreateInstance
method.
string className = this.GetType().Name;
In this example, the GetType
method is called on the current instance of the class. The Name
property is called on the resulting Type
to get the name of the class as a string.
string className = "MyClass"; object instance = Activator.CreateInstance(Type.GetType(className));
In this example, the Type.GetType
method is called with the name of the class as a string to get the Type
object for the class. The Activator.CreateInstance
method is called with the Type
object to create an instance of the class. The resulting object
can be cast to the appropriate type if necessary. Note that if the class is not in the current assembly, you may need to specify the assembly name as well.
C# get class name
string className = typeof(MyClass).Name;
Getting the name of the current class in C#
This can be achieved using the nameof
operator:
string currentClassName = nameof(MyClass);
C# reflection get class name
Using reflection to get the class name:
string className = typeof(MyClass).FullName;
Dynamically retrieve class name in C#
Dynamically retrieving the class name at runtime:
string className = this.GetType().Name;
C# typeof get class name
Using typeof
to get the class name:
string className = typeof(MyClass).Name;
Accessing class name in C# without an instance
You can access the class name without an instance using typeof
:
string className = typeof(MyClass).Name;
C# get class name from instance
If you have an instance, you can get the class name using GetType
:
MyClass obj = new MyClass(); string className = obj.GetType().Name;
Using reflection to get class name in C#
Reflection can be used to get the class name:
string className = typeof(MyClass).Name;
C# get class name as a string
Simply using the class name as a string:
string className = "MyClass";
Current class name in C# without hardcoding
Using the nameof
operator to avoid hardcoding:
string currentClassName = nameof(MyClass);
C# class name from type
Getting the class name from a Type
object:
Type type = typeof(MyClass); string className = type.Name;
Getting class name in C# using generics
Using generics to get the class name:
public class MyClass<T> { public string GetClassName() => typeof(T).Name; }
C# class name from within a method
Getting the class name from within a method:
string className = MethodInClass();
public class MyClass { public string MethodInClass() { return GetType().Name; } }
Retrieve full class name with namespace in C#
Getting the full class name with namespace:
string className = typeof(MyNamespace.MyClass).FullName;
C# get class name from instance of derived class
If you have an instance of a derived class, you can still get the base class name:
MyBaseClass obj = new MyDerivedClass(); string className = obj.GetType().Name; // Gets the base class name
Getting the simple class name in C#
Getting the simple class name without namespace:
string className = typeof(MyNamespace.MyClass).Name;
C# get class name from stack trace
Using the stack trace to get the class name:
string className = new StackTrace().GetFrame(0).GetMethod().ReflectedType.Name;
Retrieve class name with inheritance in C#
If you want the base class name even in the presence of inheritance:
string className = GetType().BaseType.Name;
C# get class name dynamically
Dynamically getting the class name:
string className = GetClassNameDynamically();
public string GetClassNameDynamically() { return GetType().Name; }
Accessing class name in C# with reflection in a static context
You can still use reflection in a static context:
string className = typeof(MyClass).Name;