Sei sulla pagina 1di 3

10 features in C# that you really should learn (and use!

1) async / await
Use the async / await-pattern to allow unblocking of the UI / current thread when
execution blocking operations. The async / await-pattern works by letting the code
continue executing even if something is blocking the execution (like a web
request).

Read more about the async / await-pattern here: https://msdn.microsoft.com/en-


us/library/hh191443.aspx
2) Object / array / collection initializers
Create instances of classes, arrays and collections easily by using the object,
array and collection initializers:
//Just some demo class
public class Employee {
public string Name {get; set;}
public DateTime StartDate {get; set;}
}

//Create an employlee by using the initializer


Employee emp = new Employee {Name="John Smith", StartDate=DateTime.Now()};
The above example can be really useful in unit testing but should be avoided in
other contexts as instances of classes should be created using a constructor.
Read more about initializers here: https://msdn.microsoft.com/en-
us/library/bb384062.aspx
3) Lambdas, predicates, delegates and closures
These features are practically a necessity in many cases (e.g. when using Linq),
make sure to actually learn when and how to use them.
Read more about Lambdas, predicates, delegates and closure here:
http://www.codeaddiction.net/articles/13/lambda-expressions-delegates-predicates-
and-closures-in-c
4) ?? (Null coalescing operator)
The ??-operator returns the left side as long as it's not null, in that case the
right side will be returned:
//May be null
var someValue = service.GetValue();

var defaultValue = 23

//result will be 23 if someValue is null


var result = someValue ?? defaultValue;
The ??-operator can be chained:
string anybody = parm1 ?? localDefault ?? globalDefault;
And it can be used to convert nullable types to non nullable:
var totalPurchased = PurchaseQuantities.Sum(kvp => kvp.Value ?? 0);
Read more about the ??-operator here: https://msdn.microsoft.com/en-
us/library/ms173224.aspx
5) $"{x}" (String Interpolation) - C# 6
A new feature of C# 6 that lets you assemble strings in an efficient and elegant
way:
//Old way
var someString = String.Format("Some data: {0}, some more data: {1}", someVariable,
someOtherVariable);

//NewWay
var someString = $"Some data: {someVariable}, some more data: {someOtherVariable}";
You can put C# expressions in between the braces, which makes this very powerful.
6) ?. (Null-conditional operator) - C# 6
The null-conditional operator works like this:
//Null if customer or customer.profile or customer.profile.age is null
var currentAge = customer?.profile?.age;
No more NullReferenceExceptions!

Read more about the ?.-operator here: https://msdn.microsoft.com/en-


us/library/dn986595.aspx
7) nameof Expression - C# 6
So the new nameof-expression might not seem important, but it really has it value.
When using automatic re-factoring tools (like ReSharper) you sometime need to refer
to a method argument by it's name:
public void PrintUserName(User currentUser)
{
//The refactoring tool might miss the textual reference to current user below
if we're renaming it
if(currentUser == null)
_logger.Error("Argument currentUser is not provided");

//...
}
This is how you should use it...
public void PrintUserName(User currentUser)
{
//The refactoring tool will not miss this...
if(currentUser == null)
_logger.Error($"Argument {nameof(currentUser)} is not provided");

//...
}
Read more about the nameof-expression here: https://msdn.microsoft.com/en-
us/library/dn986596.aspx
8) Property Initializers - C# 6
Property initializers lets you declare an initial value for a property:
public class User
{
public Guid Id { get; } = Guid.NewGuid();
// ...
}
A benefit of using property initializers is that you can not declare a set:er, thus
making the property immutable. Property initializers works great together with C# 6
primary constructor syntax.
9) as and is-operators
The is-operator is used to control if an instance is of a specific type, e.g. if
you want to see if a cast is possible:
if (Person is Adult)
{
//do stuff
}
Use the as-operator to try to cast an instance to a class. It will return null if
cast was not possible:
SomeType y = x as SomeType;
if (y != null)
{
//do stuff
}
10) yield-keyword
The yield-keyword lets you feed an an IEnumerable-interface with items. The
following example will return each powers of 2 up to the exponent of 8 (e.g. 2, 4,
8, 16, 32, 64, 128 ,256):
public static IEnumerable<int> Power(int number, int exponent)
{
int result = 1;
for (int i = 0; i < exponent; i++)
{
result = result * number;
yield return result;
}
}
yield return can be very powerful if it's used in the correct way. It enables you
to lazily generate a sequence of objects, ie. the system does not have to enumerate
the whole collection - it can be done on demand.

Potrebbero piacerti anche