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;
    }
}

Console.WriteLine(
    Utils
        .
        GetLine
        (
        )
    );

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]

31 comments:

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 https://resume-chief.com/blog/teacher-resume 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 http://www.samedayessay.com/ 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 jetwriting.com 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.

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 Content Writing Services Pakistan, then we need to investigate and after that we need to actualize a few more methods on it for the powerful yield of information.

Your pursuit to showcasing article scholars has reached a conclusion with our scholastic composing organization Assignment Help Uk . Go ahead and reach us whenever it might suit you.

Hi, I am a professional writer and provide Thesis Help in Pakistan to the struggling students. If you are one of them and want to hire a professional and affordable thesis writer feel free to reach out to me.

Our main purpose is to provide our clients with online essay help.Pay Someone To Write Your Essay This is because we realise that many students all over the world face a lot of difficulties when it comes to managing time effectively at college.

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing. anyway i am providing Law Essay Writing to uk students.

I have gone through this article named ' Walk My World Learning Event'. It was a very informative article. The main feature of this article is the way of writing so that we can easily follow the information.
섯다

I love this blog! your happiness that remains constant even if you are ill. what I always said do not lose stamina. your positivity is your strength that can fight your disease. you have to struggle with this condition for your entire life with your family.
토토

A less-trivial query arises when such a technique is referred to as from a constructor or Write My Assignment For Me. Have to the compiler simply pass the call of the elegance, considering that’s what the member is named in supply code? If so, there could be no manner to differentiate between an example constructor and a static constructor.

We are the best travel services in delhi .and provide travel services in the listed below: Travel agency for Domestic and International, Travel agency for Australia, Travel agency for Canada, Travel agency for Dubai, Travel agency for Maldives, Travel agency for Sri Lanka, Travel agency for Turkey, Travel agency for Goa, Travel agency for Himachal, Travel agency for Kashmir, Travel agency for Andaman, Travel agency for Karnatka, Travel agency for Rajasthan, Travel agency for Uttarakhand, Travel agency for Tamil Nadu, Travel agency for Madhya Pradesh, Travel agency for Punjab, Travel agency for Maharashtra, Travel agency for Calcutta, Travel agency for New Delhi' Travel agency for Domestic and International, Travel agency for Australia, Travel agency for Canada, Travel agency for Dubai, Travel agency for Maldives, Travel agency for Sri Lanka, Travel agency for Turkey, Travel agency for Goa, Travel agency for Himachal, Travel agency for Kashmir, Travel agency for Andaman, Travel agency for Karnatka, Travel agency for Rajasthan, Travel agency for Uttarakhand, Travel agency for Tamil Nadu, Travel agency for Madhya Pradesh, Travel agency for Punjab, Travel agency for Maharashtra, Travel agency for Calcutta, Travel agency for New Delhi.

Head outside in our choice of waterproof and protected rock walking tall jacket for sale for men. Shop men's climbing coats, ski coats, lightweight coats and more here.

Paper composing organizations are totally lawful. As a matter of fact, certain individuals accept that it is perfect to enlist somebody for 'Write My Essay | Quality Papers by EssayEmpire.Co.UK' administrations. That's what by doing, you can zero in on different assignments while the paper author goes about your responsibilities.

Love your well written article. This article is very helpful and useful. Thanks for sharing this article. Now it's time to avail Locksmith in Leeds for more information.

disaster on your hands. That's where hasten contracting comes in. We're experts at soil SOIL stabilization services in USAstabilization, and we can help keep your land safe from flooding and landslides. Contact us today to get started

Businesses are unablBack to Back LC transactions in Dubai to expand and grow due to a shortage of working capital.

recognition of brands whileDota2 Middle East in Dubai promoting brands. Some of the most popular games on the market right now

The [CallerLineNumber] attribute can be a useful tool for debugging and understanding code flow. However, it's important to note its limitations and potential corner cases. It's always a good idea to consult with experienced developers, like those at leadingit company in Dubai, to ensure best practices are being followed.

Good tips to learn coding thanks for sharing this post for us. keep share more useful information like this. if you want law support for you kindly visit our site. thanks. Suffolk Conducción imprudente

outstanding post and i want to know more information about this blog. i really happy to read this post...
Abogados de bancarrota del capítulo 7 cerca de mí

Post a Comment