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]

25 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.

Our expenses are exceptionally low that you can without much of a stretch affort it. Presently there is no compelling reason to take out a second home loan on your home just to manage the cost of expert fixes. foundation repair Edmonton Our vision is to put stock in genuineness and giving our clients the best administrations. Our business is focused on our clients and their necessities. Setting aside you cash is important for our answer.9

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 specialise in providing ecommerce website design, Magento development, digital marketing services houston and Local SEO services in Houston.

we are a leading ecommerce marketing chicago agency providing magento development, ecomerce web design, search engine optimiztion and digital marketing services in Chicago

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.

Post a Comment