When you learn a new language you tend to settle into using it in a particular way. So if you missed a little known feature it is unlikely you will suddenly find it unless someone points it out. Here are 5 of my favorite C# features that you might not already be using.
1 – ?? Operator
The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand. So instead of using code like this…
if (reference == null)
return _defaultInstance;
else
return reference;
You can just write the following statement instead…
return reference ?? _defaultInstance;
Much cleaner and it even works for nullable value types.
2 - DebuggerStepThrough
This attribute can be added to a method or property accessor and used to speed up debugging. When debugging and using step-into the debugger will step past the method with this attribute. This is really useful in preventing you stepping in and out of the many one liners in the code. To apply do this…
public string Name { [DebuggerStepThrough] get { return _name; } [DebuggerStepThrough] set { _name = value; } }
3 – Automatic Properties
Sick and tired of adding get/set code for simple properties? This is no longer a problem with the automatic properties added in C# 3.0. Instead of this…
private string _name; public string Name { get { return _name; } set { _name = value; } }
…just do this…
public string Name { get; set; }
4 - Object Initializers
Also introduced in C# 3.0 are object initializers. This allows you to set property values inside the new statement. Without this you either create additional overloaded constructor with the possible set of initial values or you write the long hand like this…
Employee emp = new Employee();
emp.Name = "John Smith";
emp.Age = 10;
…but this is clearer…
Employee emp = new Employee {Name="John Smith", Age=10}
5 - Constant flags
Want to specify a float instead of a double for a constant? Most people know the ‘f’ modifier after a constant number informs the compiler to make it a single float value. But did you know the full list of others flags?
100m (decimal)
100f (float)
100d (double)
100u (uint)
100l (long)
100ul (ulong)
Let me know if I have missed one you really like.
October 13th, 2008 at 6:49 pm
Hi! I’d like to note that you can also concatenate more than two “??” statements, like in
instance = one ?? two ?? three ?? default;
which sets “instance” to the first of “one”, “two” and “three” which is not null. Otherwise it uses “default”.
In C# 3.0 you can also initialize arrays in a new way:
int[] numbers = { 1, 3, 5, 7, 9 };
instead of
int[] numbers = new int[] { 1, 3, 5, 7, 9 };
October 13th, 2008 at 7:03 pm
Very interesting! Thanks for sharing.
C# 3.0 goodies don’t require .NET 3.0. They only require VS2008. Of course, some of the new stuff (such as LINQ) requires .NET 3.
Regarding automatic properties, in your example, what’s the purpose? What does it achieve vs simly having a variable?
October 13th, 2008 at 7:28 pm
Good list Phil!
For the VB’ers out there, here’s some translations:
1. Return If(reference, _defaultInstance)
Yep, the If looks pretty dang terrible, but that’s the blessed way to do a null coalesce in VB.Net
2. Replace square brackets with pointy brackets
3. Us poor old VB coders don’t get automatic properties. Dang!
4. Dim emp As Employee = New Employee With {.Name = “John Smith”, .Age=10}
Note the dots, that’s the big difference when doing copy paste translations
5. Er… no idea about this one. It reminds me too much of the oldskool VB data type characters that my brain shuts down reading it :-)
October 16th, 2008 at 4:01 am
A secret keyword to invoke functions that have varargs (e.g. C language) parameters: __arglist. However, on VB it not possible.
October 16th, 2008 at 10:59 pm
Nice list Phil…lots more goodies can be found here too — http://stackoverflow.com/questions/9033/hidden-features-of-c#9125
October 16th, 2008 at 11:29 pm
Yeah good stuff, Serge: it’s that you have a public contact that says here is a property.
today it might just be an int, but later…. if you find a need to do more you can modify the internal implimentation w/o breaking the public contract.
that’s why.
May 28th, 2009 at 7:49 am
Самый интересный web сайт о новинках коммуникаторах, а также тесты сотовых устройств