Yesterday we discussed about “CallerMemberName” attribute. If you didn’t read it, you can find it here: “What’s New in C# 5.0 - Learn about CallerMemberName Attribute”.

 

Today we will discuss about another attribute named “CallerLineNumberAttribute” that has been newly added in latest CSharp version i.e. C# 5.0. Tune in to learn about it’s feature and uses to trace our development issues. Don’t forget to ask your queries and leave a comment below if this post was helpful for you.

 

What is CallerLineNumber in C# 5.0?

[CallerLineNumber] is an attribute introduced in C# 5.0, which allows you to obtain the line number in the source file at which the method was called. You can find this attribute named “CallerLineNumberAttribute” under the namespace System.Runtime.CompilerServices.

 

Remember that, you can apply the [CallerLineNumber] 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.

 

To know more about C# Named and Optional Parameters, read this post: “C# 4.0 New Features – Named and Optional Parameters”. Also, don’t forget to check out the InfoGraphic of C# Evolution.

 

Play with the Code

I hope you understood the basic concepts of [CallerLineNumber] attribute from the above section which has been introduced in C# 5.0. Let’s see it in action by following a simple console application code. We will use the same application that we used in our last post “What’s New in C# 5.0 - Learn about CallerMemberName Attribute” and modify it accordingly as per our need..

 

For this post, we will modify the “Trace” class method named “Write()” by adding a new optional parameter “lineNumber” as integer and mark it with the attribute [CallerLineNumber"]. We will also update the print command “Console.WriteLine()” to print out the line number formatted properly in the screen.

 

Here is the code snippet of the same, which you can understand very easily:

 

 

Nothing else. Now we need to run the console application to see the output of the above code. When you run it, you will see the following result where it printed out the line numbers twice as we called it from two different place:

 

image

 

Check out the below screenshot from where you will get the exact information about the line numbers:

 

image

 

In the above screenshot, you will notice that the first Trace.Write() method was called from Main() method at line number: 21. Then the second call happened when we set the property value at line number 16.

 

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 line number of the executed code especially the exceptions. 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.