4 ms·
nameof(x) ... I like that. Any performance impacts? Sounds like some reflection would be required to do that behind the scenes ...
by guiomie 12y ago
nameof(x) ... I like that. Any performance impacts? Sounds like some reflection would be required to do that behind the scenes ...
- ppog 12y agoIt's resolved at compile time. It's exactly equivalent to the literal "x", but enables refactoring tools to pick up renames, compilers to error if the name is typoed, etc.
- _pmf_ 12y agoI've always wondered why a feature with so much benefits and such low implementation overhead has not been implemented a long time ago.
- akoeplinger 12y agoThere are many edge cases you need to consider, so a seemingly simple language feature can become hard to get right quickly. I highly recommend reading through https://roslyn.codeplex.com/discussions/570551 https://roslyn.codeplex.com/discussions/570551 and the other four revisions of the nameof() spec, they do a very good job at explaining the tradeoffs of each design.
- darklajid 12y agoActually I doubt that. Haven't read the specifics, but for me that sounds like a compile time feature. C# already has a ton of those, the most prominent so far are extension methods. Foo.Blergh() can be implemented by a class that offers static void Blergh(this Foo foo) {} and the compiler will just replace the call above, looking like a specific thing Foo can do with SupportingClass.Blergh(thatFooInstance) I'd say the nameof() implementation is most likely similar and will just replace the name of the local symbol with the symbol itself, during the compilation.
- SideburnsOfDoom 12y ago> a compile time feature. C# already has a ton of those, the most prominent so far are extension methods. Yes, nameof is compile-time. Similarly, the ?. generates code (a "? :" or if statement) and so does the string interpolation (a call to string.Format). I've even sketched an outline of a talk of the c# language features that are "just" sugar. e.g. walking through a statement like var evens = numbers.Where(x => x % 2 == 0); and showing what it looks like without the compile-time sugar features like the lambda, extension method use of "Where", "var" inference, etc. Of interest I suppose to just-beyond-beginner devs
- darklajid 12y agoDid you document that somewhere? I might be interested to share that internally. We're C# shop, but quite a number of my coworkers aren't exactly up to speed and a "Let's talk about 3.5 and upwards" session is planned in a couple of weeks..
- SideburnsOfDoom 12y agoThat talk is not very mature yet ;) If I ever give it then there will be be slides.
- taspeotis 12y agoI believe this feature has been removed. There was no conclusion over what nameof(_class.Property) should return.
- Locke1689 12y agoNope, it's in.
- endeavour 12y agoWhy would it need reflection? The name is there in the source code right in front of you.