C# 5.0 comes with Visual Studio 2012 and .NET Framework 4.5. Currently it is in RC (Release Candidate) mode and will get the final RTM version soon. In our last CSharp blog post “Evolution of C# (1.0 – 5.0)”, we learned about the evolution of C# and also we also came to know about the main features introduced in each C# version.

 

Today in this post, we will learn about one new attribute that has been introduced in C# 5.0 named “CallerMemberName” for developers. Yes, it is for developers to trace the information about the caller code. Let’s begin discussing about it.

 

What is CallerMemberName in C# 5.0?

[CallerMemberName] is an attribute introduced in C# 5.0, which allows you to obtain the method or property name of the caller to the method. You can find this attribute named “CallerMemberNameAttribute” under the namespace System.Runtime.CompilerServices.

 

You can use the [CallerMemberName] attribute to avoid specifying the member name as a String argument to the called method. This is especially useful for the following tasks:

    • Using tracing and diagnostic routines.
    • Implementing the INotifyPropertyChanged interface when binding data. This interface allows the property of an object to notify a bound control that the property has changed, so that the control can display the updated information. Without the [CallerMemberName] attribute, you must specify the property name as a literal.

The following table shows the member names that are returned when you use the [CallerMemberName] attribute:

 

Origin Member Name Result
Method, property or event The name of the method, property or event
Constructor The string ".ctor"
Static constructor The string ".cctor"
Destructor The string "Finalize"
User-defined operators or conversions The generated name for the member
Attribute constructor The name of the member to which the attribute is applied
No containing member The default value of the optional parameter

 

Remember that, you can apply the [CallerMemberName] attribute to an optional parameter that has a default value. You must specify an explicit default value for the optional parameter. You can't apply this attribute to parameters that aren't specified as optional.

 

Play With the Code

I hope, you understood the new attribute named CallerMemberName introduced in C# 5.0. Let’s see it in action by following a sample code. First of all, create a console application targeting .NET 4.5 Framework. Now for this example, we will create a static class named “Trace” for our example and add a new static method named “Write” with an optional parameter “memberName”.

 

As shown below, mark the optional parameter with [CallerMemberName” attribute:

 

Learn about CallerMemberName Attribute in C# 5.0

 

The method implementation will print the member name as demonstrated above using the Console.WriteLine() method.

 

Now let us open our main “Program” class and introduce a new property. The setter of the property will call the method Trace.Write(). Now we will first call the said method from the Main() method and then we will set the property value as shown below:

 

Example: Learn about CallerMemberName Attribute in C# 5.0

 

When you run the above code, it will first call the Trace.Write() method from the Main() and print the name of the Main() method in the output screen. Now, it will set the property value and the setter method inside the property will print the name of the property in the screen.

 

Here is the console output screen of the above code:

 

Example Output: Learn about CallerMemberName Attribute in C# 5.0

 

I hope that the functionality of the above attribute is clear to you now. You can now think of the use of this attribute in your code to trace the caller of the executed line. If you have further question, just drop a line below and I will try to answer you as soon as possible.

 

Last but not least, connect with me on Twitter and Facebook for technical updates and articles news. Also subscribe to my blog’s Newsletter to get all the updates delivered directly to your inbox. We won’t spam or share your email address as we respect your privacy.

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.