Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

C# in Depth, Fourth Edition: the definitive guide to the C# programming language


Published on

C# is an amazing language that's up to any challenge you can throw at it. As a C# developer, you also need to be up to the task. C# in Depth, Fourth Edition is your key to unlocking the powerful new features added to the language in C# 5, 6, and 7.
If you're a C# developer who's comfortable working with the language and wants to really dig in deep, then this book is for you.
Save 42% off C# in Depth, Fourth Edition with code deepcsharp at:

Published in: Software
  • D0WNL0AD FULL ▶ ▶ ▶ ▶ ◀ ◀ ◀ ◀
    Are you sure you want to  Yes  No
    Your message goes here

C# in Depth, Fourth Edition: the definitive guide to the C# programming language

  1. 1. Making more robust and change-tolerant code with nameof. Save 42% off C# in Depth, Fourth Edition with code deepcsharp at
  2. 2. So, what is the new nameof operator and what does it do? The new nameof operator is trivial to describe: it takes an expression referring to a member or local variable, and the result is a compile-time constant string with the simple name for that member or variable. It’s really that simple. Any time you hard-code the name of a class, property, or method, you’ll be better off with the nameof operator – your code will be more robust, both now and in the face of changes. Let’s see how it works…
  3. 3. Here’s an example, printing out the names of a class, method, field and parameter.
  4. 4. You might be asking yourself: why would I want to use this over string literals? You can achieve similar results using string literals, and in the case of the previous example, the code would have been shorter. So why is it better to use nameof? In one word, robustness. If you make a typo in a string literal, there’s nothing to tell you – whereas if you make a typo in a nameof operand, you’ll get a compile-time error. Not only will the compiler tell you if you get things wrong, but it “knows” that your nameof code is associated with the member or variable you’re naming.
  5. 5. This means that if you rename things in a refactoring- aware way, your nameof operand will change too! Consider the code below. Its purpose is irrelevant, but note how oldName occurs three times: for the parameter declaration, obtaining its name with nameof, and obtaining the value as just a simple expression.
  6. 6. In Visual Studio, if you place your cursor within any of the three occurrences of oldName and press F2 for the Rename operation, all three will be renamed together, as shown here.
  7. 7. The aforementioned approach works for other names – methods, types and so forth. Basically, nameof is refactoring-friendly in a way that hard- coded strings literals aren’t. So the question becomes: when should you use nameof? Let’s go over some common uses of nameof so you can get an idea of when it makes sense to use it. The examples that follow are, by no means, the only sensible uses of nameof – just some that you’re likely to come across often. Let’s have a look…
  8. 8. Argument validation Other precondition methods are used in a similar way. This is by far the most common use I find for nameof, and if you’re not already validating arguments to your public methods, I’d strongly advise you to start doing so!
  9. 9. Property change notification for computed properties. CallerMemberNameAttribute makes it very easy to raise events in INotifyPropertyChanged implementations when the property itself changes… but what if changing the value of one property has a knock- on effect for another property? For example, suppose you have a Rectangle class with read/write Height and Width properties, and a read-only Area property. It’s useful to be able to raise the event for the Area property, specifying the name of the property in a safe way, as shown in the next slide.
  10. 10. Using nameof to raise a property change notification
  11. 11. Most of this code is exactly as you might have written it in C# 5 but the line in bold would either have had to be RaisePropertyChanged("Area") or RaisePropertyChanged(() => Area). The latter approach would have been both complex in terms of the RaisePropertyChanged code, and also unfortunately inefficient, building up an expression tree solely to be inspected for the name. The nameof solution is much cleaner.
  12. 12. Attributes Sometimes attributes refer to other members, to indicate how the members relate to each other. When we want to refer to a type, we can already use typeof to make that relationship… but that doesn’t work for any other kind of member. As a concrete example, NUnit allows tests to be parameterized with values that are extracted from a field, property, or method using the TestCaseSource attribute. The nameof operator allows us to refer to that member in a safe way. The code example on the next page shows an example from “Noda Time”, testing that all the time zones loaded from TZDB behave appropriately.
  13. 13. Specifying a test case source with nameof.
  14. 14. This can be used for more than testing– it’s applicable wherever attributes indicate a relationship. You could imagine a more sophisticated RaisePropertyChanged method from a few slides ago, where the relationship between properties could be specified with attributes instead of within code: The event-raising method could keep a cached data structure that would raise a change notification for Area whenever it was notified that the Width property had changed.
  15. 15. It’s also not uncommon to have two properties in a class: one for a foreign key, and the other to be the entity that key represents. For example: There are no doubt many other attributes which can take advantage of this approach. Now you’re aware of it, you may well find there are places in your existing codebase which will benefit from nameof.
  16. 16. Learn C# inside and out. Hopefully your interest in a deep dive into C# has been kindled. Save 42% off C# in Depth, Fourth Edition with code deepcsharp at Also see: