This post will discuss how to get the current line number in C#.

C# compiler can obtain information about the caller to a method like the file path of the source code, the line number in the source code, and the member name of the caller using info attributes.

 
Starting with .NET 4.5, you can use the CallerMemberName, CallerFilePath and CallerLineNumber attributes defined in the System.Runtime.CompilerServices namespace. The CallerLineNumber attribute obtains the line number in the source file at which the method is called. For example, the following code shows how to use their usage.

Download  Run Code

Output:

Message: Hello
Source Line Number: 19
Caller Function: Main
Source file path: = /box/Main.cs

That’s all about getting the current line number in C#.