What’s New in C# 5.0 - Learn about CallerLineNumber Attribute


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.


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

6 comments

  1. I really appreciate the kind of topics you post here. Thanks for sharing information that is actually helpful. Good day!
    http://www.fadeplex.com

    ReplyDelete
  2. Hi,

    Thanks for the post, really helpful. As you say it is going to be very helpful while logging exceptions. But I have a doubt, for capturing exceptions we would be using try and catch blocks. Now when we call the Trace.Write() method in the catch block to log the line in which the exception occurred and the exception message, how do we know in which line in the try block did the exception occur since the Trace.Write() method is going to be called in the catch black? Can you suggest me a solution? Once again thanks.

    ReplyDelete
  3. hey that's really a great post and i like this and thanks for sharing it with us!I have read a few of the articles on your website now because I was looking for information aboutgames App. and I really like your style of blogging.

    ReplyDelete
  4. Great blog!! You should start many more. I love all the info provided. I will stay tuned :)Send Gifts To Pakistan

    ReplyDelete
  5. I just read through the entire article of yours and it was quite good. This is a great article thanks for sharing this information. I will visit your blog regularly for some latest post. how to hack

    ReplyDelete
  6. Just want to say your article is amazing & your info about C# is awesome. Many Thanks.
    Send Gifts to Pakistan

    ReplyDelete


 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top