---
title: "C# 6.0 - Saving Developers From Themselves - Muhammad Rehan Saeed"
description: "C# 6.0 helps reduce human error and save developers from themselves using the nameof operator, string interpolation and the null-conditional operator."
canonical_url: "https://rehansaeed.com/c-6-0-saving-developers-from-themselves/"
last_updated: "2015-05-10T00:00:00+00:00"
meta:
  author: "Muhammad Rehan Saeed"
  keywords: ".NET,C#,C# 6.0,INotifyPropertyChanged,nameof"
  "og:description": "C# 6.0 helps reduce human error and save developers from themselves using the nameof operator, string interpolation and the null-conditional operator."
  "og:image": "https://rehansaeed.com/_og/s/o_c5e00997bb2f7211.png"
  "og:image:alt": "C# 6.0 - Saving Developers From Themselves - Muhammad Rehan Saeed"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "C# 6.0 - Saving Developers From Themselves"
  "twitter:image": "https://rehansaeed.com/_og/s/o_c5e00997bb2f7211.png"
  "twitter:image:alt": "C# 6.0 - Saving Developers From Themselves - Muhammad Rehan Saeed"
---

> Index of every page on this site: [llms.txt](https://rehansaeed.com/llms.txt)

# [C# 6.0 - Saving Developers From Themselves](https://rehansaeed.com/c-6-0-saving-developers-from-themselves/)

Posted 10 May 2015 - **5 min read**

![Microsoft .NET](https://rehansaeed.com/_ipx/f_png&s_2560x1440/images/hero/Microsoft-.NET-1366x768.png)

# What's New in C# 6.0 Link to this section

If you haven't already taken a look at what's new in C# 6.0, you should certainly read [this](https://msdn.microsoft.com/en-us/magazine/dn802602.aspx) article. This blog post is going to cover how C# 6.0 can help reduce the number of bugs in your code by giving you the tools to avoid common developer mistakes.

In my opinion, the changes introduced in C# 6.0 can be split into two separate groups. The first group of changes seems to be a declaration of war on curly braces (`{}`), you can now omit them in many cases. I personally am not too sure about this set of features, it reduces the lines of code you have to write a little and may save a few seconds but at the cost of having to learn a new set of syntax. If you cast your mind back to being a newbie developer (or if you are one), lots of syntax to remember can be difficult to deal with.

This is a problem that C++ developers know well, C++ is a pretty old language now but is still undergoing rapid development with C++ 11, 14 and beyond. It's got to the stage where there are so many ways to skin a cat in C++, even experienced developers can be slowed down when looking at code using older patterns and paradigms. The C# caretakers need to be careful that each new feature is genuinely worth the effort and not just bloat.

The second set of features is what I am really interested in. These are features which will genuinely save you from yourself. They will stop developers making many common mistakes.

# The nameof Operator Link to this section

The `nameof` operator simply gives you the name of any type you pass into it. You can take a look at the simple example below:

```cs
string obiwan;  
Console.WriteLine(nameof(obiwan));
// Prints obiwan
  
int kenobi = 2;
Console.WriteLine(nameof(kenobi));
// Prints kenobi
```

## Argument Exceptions Link to this section

So where can this help us? Well, I can think of a few examples, the first being throwing argument exceptions. Argument exceptions all take a parameter, which represents the name of the invalid parameter. In the past, we had to pass this as a string. The problem was that the parameter might get renamed and you might forget to update the string to reflect that.

```cs
public void FightCrime(string hero)  
{
    if (hero == null)  
    {
        throw new ArgumentNullException("hero");
    }

    // Omitted crime fighting code...  
}
```

```cs
public void FightCrime(string hero)  
{
    if (hero == null)  
    {
        throw new ArgumentNullException(nameof(hero));
    }

    // Omitted crime fighting code...  
}
```

With the second example, if you used Visual Studio to rename the hero parameter, then the hero in the `nameof` operator will also be updated.

## INotifyPropertyChanged Link to this section

This interface is notorious if you are doing any WPF/Silverlight/WinRT/XAML development, for requiring strings to be passed to it. With the `nameof` operator, this becomes a thing of the past.

```cs
public class Ship : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;   
    
    private string name;
    
    public string Name
    {
        get { return this.name; }
        set
        {
            this.name = value;
            this.OnPropertyChanged(nameof(this.Name));
        }
    }
    
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler eventHandler = this.PropertyChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
```

## ASP.NET MVC Link to this section

ASP.NET MVC makes massive use of strings everywhere. This is a massive problem when you want to rename something. In fact, I've taken to using constants everywhere. It's more work to setup but in the long run its much easier to maintain. Here is an example of how we can use `nameof` to create a link and do away with strings and constants:

```cs
@Html.ActionLink("Home", "Index", "Home")

@Html.ActionLink2("Home", nameof(HomeController.Index), nameof(HomeController))

public static MvcHtmlString ActionLink2(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
{
    htmlHelper.ActionLink(linkText, actionName, controllerName.Substring(0, controllerName.Length - 10));
}
```

The above example is a little contrived. In the real world, I would never use `ActionLink` and use `RouteLink` instead. Naming your routes has better performance and is just easier to understand when you have multiple routes with the same name (for GET and POST requests).

# String Interpolation Link to this section

I think we've all used `string.Format` and got our arguments in the wrong positions or entered the index numbers incorrectly at some point in time. Well, that bug is now a thing of the past.

```cs
// Before C# 6.0 String Interpolation
string nameAndAge = string.Format("Name:{0}, Age:{1}", name, age);
// After C# 6.0 String Interpolation
string nameAndAge = $"Name:{name}, Age:{age}";
```

As you can see, you can now use your parameters directly in the strings with full syntax highlighting and renaming support too. In fact the C# 6.0 code actually compiles down to doing a `string.Format` behind the scenes.

# The Null-Conditional Operator Link to this section

Every C# developer has at some point stared at the text from a `NullReferenceException` and thought in their head, this is a really rubbish message and leaves out vital information. In fact there is [this](https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2371587-better-nullpointerexception-error-message) post on UserVoice, asking Microsoft to improve their `NullReferenceException` messages. It turns out that Microsoft has thought of this, they haven't improved the message (They still should, please up-vote the UserVoice post) but they have introduced the Null-Conditional operator.

```cs
public string Truncate(string value, int length)
{
    string result = value;
    if (value != null)
    {
        result = value.Substring(0, Math.Min(value.Length, length));
    }
    return result;
}
```

```cs
public string Truncate(string value, int length)
{          
    return value?.Substring(0, Math.Min(value.Length, length));

    // Wow, look at all this code I didn't have to write!
}
```

# Conclusions Link to this section

As you can see there is a common theme with two of the three C# 6.0 features I've picked above. They give us tools to better deal with strings which have terrible IDE and language support. Making a typo in a string gives us no compile time errors and Visual Studio doesn't help either. Each of these features has allowed us to deal with strongly typed objects instead, which have full language and IDE support.

The Null-Conditional operator is another great tool to help mitigate really common but minor bugs that catch even the most experienced developers out. These are great features, and should help stop silly mistakes that all of us developers make. We are after all, human.

## Web Mentions

[What's this?](https://en.wikipedia.org/wiki/Webmention)

### 0 Linked

### 0 Replies

## GitHub Comments

### 8 Comments

[Comment on GitHub](https://github.com/RehanSaeed/rehansaeed.github.io/issues/61)

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Saif](https://camo.githubusercontent.com/5529fa47a94659947eff252a625ab0feeb4ca3c4eb902141ef70e24f9d1d6394/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f65383030653565353634366132656362353165383364613966373435373664653f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/5529fa47a94659947eff252a625ab0feeb4ca3c4eb902141ef70e24f9d1d6394/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f65383030653565353634366132656362353165383364613966373435373664653f733d393626643d6d6d26723d67) **[Saif](http://www.flux.pc.uk/)** commented on **2015-05-23 13:43:31**

I've started reading some of your blog posts, and some are quite interesting . Keep it up!

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![BlueInt32](https://camo.githubusercontent.com/8686b116cc93d112ba9ff5c0954d61dcdf73caf46824f0fbeb38c79a79b4ed34/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f66336666333737623536653430663964636431636462636331636131313466343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8686b116cc93d112ba9ff5c0954d61dcdf73caf46824f0fbeb38c79a79b4ed34/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f66336666333737623536653430663964636431636462636331636131313466343f733d393626643d6d6d26723d67) **BlueInt32** commented on **2015-06-09 20:55:37**

Good article, thanks !

[http://contrastrebellion.com](http://contrastrebellion.com/) though...

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2015-06-10 08:18:51**

> Good article, thanks !
>
> [http://contrastrebellion.com](http://contrastrebellion.com/) though...

Thanks, I've now increased the contrast. Let me know what you think.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Frank Fajardo](https://camo.githubusercontent.com/79dd6a0d35bcab07a2a4a93f1432f1f753c6301d3e147489b00f656acaf2258d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f63333664343836373732353465343337613339633262663461646365663739333f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/79dd6a0d35bcab07a2a4a93f1432f1f753c6301d3e147489b00f656acaf2258d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f63333664343836373732353465343337613339633262663461646365663739333f733d393626643d6d6d26723d67) **Frank Fajardo** commented on **2015-07-20 01:08:21**

I like the use of `nameof` on controller and method names. As for the string interpolation, would that cause an issue when refactoring?

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2015-07-20 07:04:07**

> I like the use of `nameof` on controller and method names. As for the string interpolation, would that cause an issue when refactoring?

That's what I thought initially, until I used the feature in Visual Studio. The parameters in the string are not actually strings (They are not highlighted red in VS) but are still parameters. Renaming the variable will still have an effect on the name of the parameter in the string.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Frank Fajardo](https://camo.githubusercontent.com/79dd6a0d35bcab07a2a4a93f1432f1f753c6301d3e147489b00f656acaf2258d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f63333664343836373732353465343337613339633262663461646365663739333f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/79dd6a0d35bcab07a2a4a93f1432f1f753c6301d3e147489b00f656acaf2258d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f63333664343836373732353465343337613339633262663461646365663739333f733d393626643d6d6d26723d67) **Frank Fajardo** commented on **2015-07-21 23:55:17**

> That's what I thought initially, until I used the feature in Visual Studio. The parameters in the string are not actually strings (They are not highlighted red in VS) but are still parameters. Renaming the variable will still have an effect on the name of the parameter in the string.

Thanks Rehan! Great post.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Frank Fajardo](https://camo.githubusercontent.com/79dd6a0d35bcab07a2a4a93f1432f1f753c6301d3e147489b00f656acaf2258d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f63333664343836373732353465343337613339633262663461646365663739333f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/79dd6a0d35bcab07a2a4a93f1432f1f753c6301d3e147489b00f656acaf2258d/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f63333664343836373732353465343337613339633262663461646365663739333f733d393626643d6d6d26723d67) **Frank Fajardo** commented on **2015-07-24 01:55:07**

> That's what I thought initially, until I used the feature in Visual Studio. The parameters in the string are not actually strings (They are not highlighted red in VS) but are still parameters. Renaming the variable will still have an effect on the name of the parameter in the string.

One thing I think they also introduced are the **asp-controller** and **asp-action** attributes to specify which controller and method to call, like so:

```
<a asp-controller="Home" asp-action="Index" rel="nofollow">Home</a>
```

I assume you can remove the use of strings here and instead use the `nameof()` operator like you did in the `ActionLink`.

One thing that just clicked in my mind: would `nameof(HomeController)` return `Home` and not `HomeController`? Where did it get the name from? Or was it determined out of (MVC route) convention?

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2015-07-24 07:36:03**

> One thing I think they also introduced are the **asp-controller** and **asp-action** attributes to specify which controller and method to call, like so:
>
> ```
> <a asp-controller="Home" asp-action="Index" rel="nofollow">Home</a>
> ```
>
> I assume you can remove the use of strings here and instead use the `nameof()` operator like you did in the `ActionLink`.
>
> One thing that just clicked in my mind: would `nameof(HomeController)` return `Home` and not `HomeController`? Where did it get the name from? Or was it determined out of (MVC route) convention?

That's correct. You need to do something like:

```
nameof(HomeController).SubString(0, nameof(HomeController).Length - 10);
```

You can create a quick HTML helper for this or a tag helper in MVC 6.

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://rehansaeed.com/#website","@type":"WebSite","description":"Software Developer at Microsoft, YouTuber, Open Source Contributor and Blogger","inLanguage":"en-GB","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/","author":{"@id":"https://rehansaeed.com/#identity"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://rehansaeed.com/?search={search_term_string}"},"query-input":"required name=search_term_string"}],"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/c-6-0-saving-developers-from-themselves/#webpage","@type":"WebPage","dateModified":"2015-05-10T00:00:00+00:00","datePublished":"2015-05-10T00:00:00+00:00","description":"C# 6.0 helps reduce human error and save developers from themselves using the nameof operator, string interpolation and the null-conditional operator.","name":"C# 6.0 - Saving Developers From Themselves","url":"https://rehansaeed.com/c-6-0-saving-developers-from-themselves/","breadcrumb":{"@id":"https://rehansaeed.com/c-6-0-saving-developers-from-themselves/#breadcrumb"},"isPartOf":{"@id":"https://rehansaeed.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://rehansaeed.com/c-6-0-saving-developers-from-themselves/"]}]},{"@id":"https://rehansaeed.com/#identity","@type":"Person","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/about/","image":{"@id":"https://rehansaeed.com/#/schema/image/1"},"sameAs":["https://github.com/RehanSaeed","https://www.linkedin.com/in/muhammad-rehan-saeed","https://stackoverflow.com/users/1212017/muhammad-rehan-saeed","https://www.youtube.com/c/MuhammadRehanSaeed","https://www.twitch.tv/rehansaeeduk"]},{"@id":"https://rehansaeed.com/#publisher","@type":"Organization","logo":"https://rehansaeed.com/images/schema/Publisher-600x60.png","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com"},{"@id":"https://rehansaeed.com/c-6-0-saving-developers-from-themselves/#article","@type":"Article","dateModified":"2015-05-10T00:00:00+00:00","datePublished":"2015-05-10T00:00:00+00:00","description":"C# 6.0 helps reduce human error and save developers from themselves using the nameof operator, string interpolation and the null-conditional operator.","headline":"C# 6.0 - Saving Developers From Themselves","inLanguage":"en-GB","url":"https://rehansaeed.com/c-6-0-saving-developers-from-themselves/","author":{"@id":"https://rehansaeed.com/#identity"},"image":[{"@type":"ImageObject","url":"https://rehansaeed.com/images/hero/Microsoft-.NET-1366x768.png","caption":"Microsoft .NET","width":"1366","height":"768"}],"isPartOf":{"@id":"https://rehansaeed.com/c-6-0-saving-developers-from-themselves/#webpage"},"keywords":[".NET","C#","C# 6.0","INotifyPropertyChanged","nameof"],"mainEntityOfPage":{"@id":"https://rehansaeed.com/c-6-0-saving-developers-from-themselves/#webpage"},"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/c-6-0-saving-developers-from-themselves/#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Blog","item":"https://rehansaeed.com/","position":1},{"@type":"ListItem","name":"C# 6.0 - Saving Developers From Themselves","item":"https://rehansaeed.com/c-6-0-saving-developers-from-themselves/","position":2}]},{"@id":"https://rehansaeed.com/#/schema/image/1","@type":"ImageObject","contentUrl":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png","inLanguage":"en-GB","url":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png"}]}
```

## Sitemap

See the full [sitemap](https://rehansaeed.com/sitemap.md) for all pages.
