Sharp-wishes-nameof

  1. published 2010-05-04

Sharp wishes: `nameof`

Hi again, after over half a year of silence i think i am finally ready to write something again. The thing is: i have been writing a lot of C# lately as part of my job.

When you work some years with a language you start to envision certain improvements to the langauge which would make your life much easier. I plan to write these findings here as i find the time to do so.

The first thing i wish to tackle is the "lack of" a nameof operator, this is actually something which is lacking in Java and C++ too, but i find that it is more needed in C# because you deal with a lot of strings there, not to say that it would not be a huge boon in any language.

What this new operator tries to tackle is a problem most C# developers have faced: you have a class with a number of properties, these properties can be updated which, in turn, makes the object instance emit a PropertyChanged event for that particular property.

The problem comes when you want to write code which reacts to this property change for a particular property:

{{{#!highlight csharp
...
private void object_PropertyChanged ( object sender, PropertyChangedEventArgs args )
{
if ( args.PropertyName == "NumberOfSamples" )
{
// Do stuff
}
}
...
}}}

This works ok at the beginning, but you are actually introducing dynamic/runtime programming into your statically compiled program. What happens if NumberOfSamples changes name to NumberOfActualSamples ? Your code will be broken but you will not see it on compile time, only on runtime.

Sometimes people, including myself, handle this by introducing an inner public class called Properties which contain several public const strings with the property names:

{{{#!highlight csharp
public class MyClass
{
public class Properties
{
public const string NumberOfSamples = "NumberOfActualSamples";
}
}}}

{{{#!highlight csharp
private int m_numberOfSamples;
public int NumberOfSamples
{
get
{
return m_numberOfSamples;
}
set
{
if ( m_numberOfSamples != value )
{
m_numberOfSamples = value;
}}}

{{{#!highlight csharp
OnPropertyChanged ( Properties.NumberOfSamples );
}
}
}
}
}}}

This is actually quite a nice way to write code, you even get the added benefit of being able to search your code for places where a particular property is used from inside visual studio by using the built-in "Find all references" command.

But please notice that this is a violation of the DRY (Don't Repeayt Yourself) principle, that all hackers hold so dear.

A further complication is that this does not work for interfaces where inner classes are not allowed.

So what to do?

My proposal is to add a "nameof" operator, which takes the name of an identifier and returns it's name as a string. The "OnPropertyChanged" part of the code above would then look like:

{{{#!highlight csharp
:::csharp
...
OnPropertyChanged ( nameof(NumberOfSamples) );
...
}}}

This syntax looks pretty easy to me, nameof works pretty much the same as typeof.

Of course you would have to make a few adjustments to be able to do things like:

I am sure you can think of more.

This is just a proposal, please report back with comments.


CategoryBlog CategorySharpWishes