If I have a class called MyProgram
, is there a way of retrieving "MyProgram" as a string?

- 42,611
- 64
- 162
- 244
11 Answers
Try this:
this.GetType().Name

- 344,730
- 71
- 640
- 635

- 18,530
- 1
- 38
- 33
-
@Thomas - True, although the OP will have to use reflection to do so. This may or may not matter. – micahtan Jan 21 '10 at 21:58
-
you could also create a static method that creates an instance and returns the Name property. Definitely easier than using Reflection. – MusiGenesis Jan 21 '10 at 22:42
-
42If you're in a static method then the *developer* knows what the name of the type is. You can just type it in as a string in the source code. – Eric Lippert Jan 22 '10 at 00:33
-
173@EricLippert: If you type the name in, the compiler won't catch it if the class is renamed. – Halvard Mar 14 '13 at 11:18
-
13@Halvard: First, if the static method is in the current type then the name is optional; if you're worried about it changing, omit it. Second, Visual Studio will automatically give you a smart tag when you rename a class that renames all instances of it. And third, if you're renaming a class then odds are good you're going to have to make a lot of changes in a lot of places already. – Eric Lippert Mar 14 '13 at 14:41
-
14@EricLippert You are right. Any ***new*** version of Visual Studio or ReSharper will catch the strings with the same name as the class being renamed. My previous comment is just some old, now useless knowledge ... – Halvard Mar 15 '13 at 09:24
-
Can the answer be updated to also show how to get the `SimpleName`? #noob – Snekse May 30 '14 at 16:23
-
2@EricLippert The reason I want to detect the name of the class (and even method) that I'm in is for logging purposes - I guess I could write the stacktrace, but it seems excessively verbose. If I'd like to write something to log, and have the log include "MyNamespace.FooClass.BarMethod" at the beginning of the WriteLine automatically, is there a good way to do this? – Edward Ned Harvey Feb 24 '15 at 21:09
-
@EdwardNedHarvey: Yes! This feature was added in C# 5. The documentation is here: https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2 – Eric Lippert Feb 24 '15 at 21:44
-
30@Halvard: ... and in C# 6 you can use the new `nameof` operator. – Eric Lippert Feb 24 '15 at 21:46
-
@EricLippert Finally :) – Halvard Feb 25 '15 at 08:24
-
1@EricLippert That works great for detecting the calling method name, but logging the filename and line number isn't what I'm looking for, because then I'd have to checkout the same source tree as the application that Trace'd something to log. I'm still looking for a good way to detect the namespace & class name. – Edward Ned Harvey Feb 25 '15 at 11:40
-
@EricLippert What about this? Is there any reason this wouldn't be a good answer? ```var meth = System.Reflection.MethodBase.GetCurrentMethod(); string methName = meth.DeclaringType.FullName + "." + meth.Name;``` – Edward Ned Harvey Feb 25 '15 at 11:53
-
I assume this wouldn't work if you use it in release and apply obfuscation – user3533716 Jun 25 '15 at 09:40
-
2You can also do `typeof(ThisClass).Name` – Bennik2000 Oct 28 '15 at 11:57
-
3@Bennik2000 - In case of an inherited class, your suggestion gives the name of the base class! – Sudhanshu Mishra Nov 18 '15 at 23:31
-
1@dotnetguy, the discussion above was about the static methods. Static methods are not inherited in C#. So in 2017 you can always use nameof() in static methods and GetType() in non-static. – Konstantin Jul 24 '17 at 18:28
-
2@Konstantin, 1) My comment is in response to Bennik2000, which is seemingly a comment on an answer to a question that does not mention static, my comment still holds. 2) inside a class you can always do nameof() irrespective of it being static or instance: https://dotnetfiddle.net/y9XzEB – Sudhanshu Mishra Jul 25 '17 at 02:54
-
11) Ah, I see now 2) I have updated fiddle to show you what I mean. – Konstantin Jul 26 '17 at 18:47
-
can exclude "this" as well – Andrew Apr 30 '19 at 18:29
-
I use the method in this Answer as it works for inheritance, if a method on A uses this to return "A", then if inherited by class B it will return "B". – Issung Feb 07 '22 at 02:46
I wanted to throw this up for good measure. I think the way @micahtan posted is preferred.
typeof(MyProgram).Name

- 9,156
- 9
- 56
- 73

- 77,506
- 18
- 119
- 157
-
37This is actually better, because: 1. It will work in static context 2. It is compile time computed, so it doesn't cost like reflection – Gilbert Dec 14 '13 at 15:38
-
2@Gilbert This is not better than the accepted answer by micatan, which works regardless of the name of the class. The only advantage of this over simply `"MyProgram"` is that the compiler will catch it if the name of the class is changed. – Jim Balter Mar 25 '14 at 19:36
-
7@JimBalter It has multiple advantages: 1. Static context. 2. The type portion will not be re-evaluated by the CLR each time - it will be written to the MSIL. 3. It protects you from someone declaring a new "GetType()". – Gilbert Apr 16 '14 at 19:48
-
That's not a response to what I wrote -- *The only advantage of this over simply "MyProgram"* – Jim Balter Apr 16 '14 at 21:31
-
2@JimBalter Correct, the advantages over "MyProgram" are the that the compiler will catch it if the name of the class is changed OR if you got it wrong the first time. – Gilbert May 19 '14 at 17:18
-
I think that there is clearly no better or worse. Which is better depends solely on circumstance. That's why we can do both. – Aelphaeis Jun 03 '14 at 17:14
-
14If you want to get inherited class name and this call is in the parent then it won't work. – Gh61 Jul 25 '14 at 09:33
-
20This has the disadvantage of having to reference the type explicitly, which makes it less easily reusable. – cprcrack Feb 16 '15 at 17:31
-
+1 this is the best answer for me and worked for a template type in a generic method which is what I was looking for – jbat100 Aug 03 '17 at 14:08
-
34
-
But you already have the name of the class. You demonstrate that by having to write it's name. You could just as easily write 'string str = "MyProgram";' Either way requires you to manually update that line of code anytime the code is copied. That completely defeats the purpose in my opinion. Reflection will give you code that is universal. – Gavin Williams Aug 25 '23 at 16:07
-
6Great stuff; also works with type _members_ (such as methods and properties) and even variables - see [the docs](https://msdn.microsoft.com/en-us/library/dn986596.aspx?f=255&MSPPError=-2147217396). – mklement0 Jun 08 '16 at 22:23
Although micahtan's answer is good, it won't work in a static method. If you want to retrieve the name of the current type, this one should work everywhere:
string className = MethodBase.GetCurrentMethod().DeclaringType.Name;

- 286,951
- 70
- 623
- 758
-
3Nice catch, although I think my method is preferred in this case. – ChaosPandion Jan 21 '10 at 21:36
-
5This Won't work for non-virtual methods, as it will return the name of the type that the method is declared and implemented in, (possibly up the inheritance chain), not the concrete type of the instance you are actually executing the code from. – Charles Bretana Jan 21 '10 at 21:56
-
2This doesn't seem to work anymore in the DNX (Dot Net Execution) framework. They removed the GetCurrentMethod() method and left only GetMethodFromHandle(). – Astaar Sep 15 '15 at 10:12
-
This is exactly what I needed to get the name of the concrete class currently executing code called from a virtual function in a descendant. – DrFloyd5 Mar 25 '20 at 17:35
If you need this in derived classes, you can put that code in the base class:
protected string GetThisClassName() { return this.GetType().Name; }
Then, you can reach the name in the derived class. Returns derived class name. Of course, when using the new keyword "nameof", there will be no need like this variety acts.
Besides you can define this:
public static class Extension
{
public static string NameOf(this object o)
{
return o.GetType().Name;
}
}
And then use like this:
public class MyProgram
{
string thisClassName;
public MyProgram()
{
this.thisClassName = this.NameOf();
}
}

- 1,080
- 2
- 16
- 16
For reference, if you have a type that inherits from another you can also use
this.GetType().BaseType.Name

- 943
- 1
- 11
- 25
Use this
Let say Application Test.exe is running and function is foo() in form1 [basically it is class form1], then above code will generate below response.
string s1 = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
This will return .
s1 = "TEST.form1"
for function name:
string s1 = System.Reflection.MethodBase.GetCurrentMethod().Name;
will return
s1 = foo
Note if you want to use this in exception use :
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace );
}

- 53,146
- 19
- 236
- 237

- 2,567
- 1
- 20
- 16
-
DeclaringType is declared `[Nullable(2)]` so you get an warning when null check are active. – Martin Oct 11 '19 at 14:50
-
this
can be omitted. All you need to get the current class name is:
GetType().Name

- 4,634
- 4
- 46
- 53
Get Current class name of Asp.net
string CurrentClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name.ToString();
-
1DeclaringType is declared `[Nullable(2)]` so you get an warning when null check are active. – Martin Oct 11 '19 at 14:51
The easiest way is to use the call name attribute. However, currently, there is no attribute class that returns the class name or the namespace of the calling method.
See: CallerMemberNameAttributeClass
public void DoProcessing()
{
TraceMessage("Something happened.");
}
public void TraceMessage(string message,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
System.Diagnostics.Trace.WriteLine("message: " + message);
System.Diagnostics.Trace.WriteLine("member name: " + memberName);
System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}
// Sample Output:
// message: Something happened.
// member name: DoProcessing
// source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
// source line number: 31

- 1,228
- 1
- 18
- 29