Wednesday 9 April 2014

New features for c# 6 part 1

This is a list of planned features for c# 6 and what they do. Its exciting to see what is coming in c# 6. This is the first part of two posts. Part 2 will cover other features coming in c# 6 which are already done at the moment.

Binary literals and digit separators

Working with long numbers, hex or binary in c# can be hard. With binary literals and digit separators it could get a lot easier. Binary literals are used to write values of types int, uint, long, and ulong. Binary literals have two possible forms: decimal and hexadecimal.

Binary literals would look like this 0b00000100, which is equal to 4 and digit separators would look like this 0xEF_FF_00_A0, which is equal to 4026466464.

I see these features as a small change for easier readable code when working with large numbers.

Expression-bodied members

Expression-bodied members are members that have a dynamic value without getting the value from a method. For instance public double Dist => Sqrt(X * X + Y * Y) normally doing this would require a method or having a property. This would be a nicer and cleaner way of doing so.

Event initializers

You can now initialize objects with events. As of now you would have to create you object and then assign events to that object. Now you can do it directly in the initialization of that object. 

new Customer { Notify += NotifyEventHandler };

Null propagation

Null propagation is also known as safe navigation operator. 

Lets say you're going to access an descendant of a given class. Our call would be Class.ChildClass.Property. Lets say for instance that ChildClass is null. We would have to check for a null reference of the Class type and ChildClass to be safe.

With the Null propagation/Safe Navigation Operator we would be able to write Class?.ChildClass?.Property without having to check for null references all the time.

I've written about the Safe Navigation Operator before

Semicolon operator

Semicolon operator is the sequential composition operator on expressions. Just as semicolon is sequential composition on statements. Thats means you can string expressions together and the "value" of the expression is the last one.

(var x = Foo(); Write(x); x * x)

Private protected

Not much to explain, you can now have a private protected method, property or member.

private protected string GetId() { … }

Params IEnumerable

You can now use IEnumerable as a params parameter on the method, for instance int Avg(params IEnumerable<int> numbers) { … } instead of an Array of ints.

Constructor Inference

When working with generics, it can be annoying that you have to define the generic type each time. Having Tuple you need to define each type when creating a new object. It would be like new Tuple<int, string, bool>(3, "three", true);. With constructor inference we will be able to do the following new Tuple(3, "three", true);

NameOf operator

The nameof will give you the name of an call and will be nice to use with diagnostics. Log("MyMethod called"); will be replaced with Log(nameof(MyMethod) + " called"); so when the method is renamed be rerfactoring or manually changed, the log message will change automatically. If you don't change it manually the solution won't compile. No more changing static names in logging messages :)

No comments:

Post a Comment