Caller Information Attributes in C# 5.0

Caller information gives information about the caller of any function. This concept  was introduced in C# 5.0 . In C# 5.0 the caller info feature has introduced and using this feature we can easily get information about caller function.

These three types of attributes that are used in tracking information..

  • CallerFilePath: Sets the information about caller's source code file.
  • CallerLineNumber: Sets the information about caller's line number.
  • CallerMemberName: Sets the information about caller member name.

 

Let us take an example:

  1. using System;  
  2. using System.Runtime.CompilerServices;  
  3. namespace ConsApp  
  4. {  
  5.     class Program  
  6.     {  
  7.         public void Call(string text, [CallerFilePath] string File_name = "",  
  8.             [CallerLineNumber] int Line = 0,  
  9.             [CallerMemberName] string member_name = "")  
  10.         {  
  11.             Console.WriteLine(text);  
  12.             Console.WriteLine(File_name);  
  13.             Console.WriteLine(Line);  
  14.             Console.WriteLine(member_name);  
  15.         }  
  16.         static void Main(string[] args)  
  17.         {  
  18.             Program Obj = new Program();  
  19.             Obj.Call("This is Caller Method");  
  20.             Console.ReadLine();  
  21.         }  
  22.     }  
  23. }  

Output 

 
 
Before the tracking information used the StactTrack to find out the tracking information of a method that was very complicated to handle.
Next Recommended Reading Attributes For Record Properties In C# 9