Subtleties of C# 5’s new [CallerLineNumber]

UPDATE: Now that the Visual Studio 11 beta has shipped with this feature implemented, I wrote a separate blog post exploring how it actually behaves in these corner cases.

This is part 2 in a series about C# 5’s new caller info attributes; see the introduction.

The [CallerLineNumber] attribute tells the compiler to use the line number of the call site instead of the parameter’s default value.  This attribute has more corner cases than [CallerFileName].  In particular, unlike the C preprocessor’s __LINE__ macro, the C# compiler inserts the line number of a parsed method call.  Therefore, it is not always clear which line a method call expression maps to.

What should this call print:

static class Utils {
    int GetLine([CallerLineNumber] int line = 0) {
        return line;


Should it print the line number that the statement started? The line in which the call to GetLine started? The line containing the parentheses for GetLine? What if it’s in a multi-line lambda expression?

There are also a few cases in which methods are called implicitly by the compiler without appearing in source code.  What should this code print?

class Funny {
    public Funny Select(Func<object, object> d,
                        [CallerLineNumber]int line = 0) {
        Console.WriteLine(line + ": " + d(d));
        return this;

var r = (from x in new Funny() 
         let y = 1 
         select 2);

This code contains two implicit calls to the Select method that don’t have a clear source line (it gets worse for more complicated LINQ queries)

In fact, it is possible to have an implicit method call with no corresponding source code at all.

Consider this code:

class Loggable {
    public Loggable([CallerLineNumber] int line = 0) { }
class SomeClass : Loggable { }

The compiler will implicitly generate a constructor for SomeClass that calls the base Loggable constructor with its default parameter value.  What line should it pass? In fact, if SomeClass is a partial class that is defined in multiple files, it isn’t even clear what [CallerFileName] should pass.

Also, what should happen in the unlikely case that a [CallerLineNumber] method is called on line 3 billion (which would overflow an int)? (This would be easier to test on Roslyn with a fake stream source) Should it give an integer overflow compile-time error?  If [CallerLineNumber] also supports byte and short parameters, this scenario will be more likely to happen in practice.

Next time: [CallerMemberName]


This information should reach more people. Kara

thank for sharing this awesome post love to read this framaroot pingpong root Root Master

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

Information from will help you to get perfect teachers resume. I had such experience recently

For the website development todays the trend of PHP and C# is being famous through all over the world, last time when I sit to build a website of 123movies legal and finds that how the logic of C# is also important with PHP as well.

Hello! For any kind of studying we should need to focus on it. We must construct or right off the bat influence an arranging of any fortress of ordering essay papers legal at online essaywriting service, then we need to investigate and after that we need to actualize a few more methods on it for the powerful yield of information. And I would like to propose you to click on this service's webpage as they always have wonderful discounts for all pupils!

C# is one of a few dialects that focus on the Microsoft Common Language Runtime (CLR). D ialects that focus on the CLR profit by highlights, for example, cross-language reconciliation and hard money lenders orange county california special case taking care of, upgraded security, a rearranged model for segment collaboration, and troubleshooting and profiling administrations.

Nepal is one of the most excellent nation to visit for nature sweethearts who are similarly looking for experiences in the lap of the Himalayas. The country sandwiched among China and India grasps eight of the ten most elevated mountains on the planet pulling in huge number of adventurers consistently to energize their soul and sense the sensation of being at the top. Business Mathematics Assignment Help

what useful and interesting information in your post. I came across a similar one in the article of guys from who helped my acquaintance programmer with solving a difficult problem. they write quality articles and in a short time

For any sort of studying we should want to awareness on it. We ought to construct or right off the bat influence arranging of any citadel of ordering essay papers felony at Can someone do my assignment for me UK? online essay writing service, then we want to investigate special case looking after, upgraded protection, a rearranged version for phase collaboration, and troubleshooting and profiling administrations.

I don't understand what admin is saying in this article anyways if you are looking for law essay help uk then get this task form our writers.

Every one of our authors hold Master's certifications, and Ph.D's. in their different fields of specialization from the best colleges on the planet. We will help you at buy cheap assignment The authors go through a progression of tests to guarantee they are awesome and can deal with any complicated tasks.

Post a Comment