This post will discuss how to get the name of the current method being executed in C#.

1. Using StackTrace Class (System.Diagnostics)

A stack trace is an ordered collection of one or more stack frames that store debugging and diagnostic information.

The following example creates a simple stack trace using the StackTrace instance. The second frame of the stack trace contains the stack frame of the current method.

Download  Run Code

 
If you don’t want to create a helper method, change the code to the following. Here the first frame represents the top of the stack, which is the last method invocation in the sequence.

Download  Run Code

Or

Download  Run Code

2. Using MethodBase.GetCurrentMethod() method

Alternatively, the MethodBase.GetCurrentMethod() method can be used, which returns a MethodBase object representing the currently executing method.

Download  Run Code

That’s all about getting the name of the current method in C#.