Class Name in C#

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.

Get the Current Class Name

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.

Create a Class Instance from Its String Class Name

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.

  1. C# get class name

    string className = typeof(MyClass).Name;
    
  2. Getting the name of the current class in C#

    This can be achieved using the nameof operator:

    string currentClassName = nameof(MyClass);
    
  3. C# reflection get class name

    Using reflection to get the class name:

    string className = typeof(MyClass).FullName;
    
  4. Dynamically retrieve class name in C#

    Dynamically retrieving the class name at runtime:

    string className = this.GetType().Name;
    
  5. C# typeof get class name

    Using typeof to get the class name:

    string className = typeof(MyClass).Name;
    
  6. Accessing class name in C# without an instance

    You can access the class name without an instance using typeof:

    string className = typeof(MyClass).Name;
    
  7. 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;
    
  8. Using reflection to get class name in C#

    Reflection can be used to get the class name:

    string className = typeof(MyClass).Name;
    
  9. C# get class name as a string

    Simply using the class name as a string:

    string className = "MyClass";
    
  10. Current class name in C# without hardcoding

    Using the nameof operator to avoid hardcoding:

    string currentClassName = nameof(MyClass);
    
  11. C# class name from type

    Getting the class name from a Type object:

    Type type = typeof(MyClass);
    string className = type.Name;
    
  12. Getting class name in C# using generics

    Using generics to get the class name:

    public class MyClass<T>
    {
        public string GetClassName() => typeof(T).Name;
    }
    
  13. 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;
        }
    }
    
  14. Retrieve full class name with namespace in C#

    Getting the full class name with namespace:

    string className = typeof(MyNamespace.MyClass).FullName;
    
  15. 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
    
  16. Getting the simple class name in C#

    Getting the simple class name without namespace:

    string className = typeof(MyNamespace.MyClass).Name;
    
  17. 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;
    
  18. 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;
    
  19. C# get class name dynamically

    Dynamically getting the class name:

    string className = GetClassNameDynamically();
    
    public string GetClassNameDynamically()
    {
        return GetType().Name;
    }
    
  20. 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;