Tuesday 22 April 2014

New features for c# 6 part 2

This is part 2 of the new features for c# 6, see part 1 here. All these features are already done for c# 6, where the part 1 is planned features.

Primary constructors

Primary constructors a shorter way to write a constructor that automatically assigns to private instance variables. The constructor is defined with the class itself as class Point(int x, int y) { … }


Auto-property initializers / Getter-only auto-properties

Another shorthand for properties that only retrieve/assign the value to a private member. Now you can do this public int X { get; set; } = x; You can also use an getter-only auto-property, which is perfect for immutable classes! public int Y { get; } = y; 


Using static members

In your using statements you can have static members to have all methods easily accessable in that class. As so using System.Console; … Write(4);


Dictionary initializer

Dictionary initializer is another short hand. new JObject { ["x"] = 3, ["y"] = 7 } would be the same as jsobj = new JSObject(); jsobj["x"] = 3; jsobj["y"] = 7;


Indexed member initializer / indexed member access

This is quite basic but really awesome. Lets take the example from before new JObject { ["x"] = 3, ["y"] = 7 }. Doing this with indexed member initializer would be new JObject { $x = 3, $y = 7 }. So object["x"] is the same as object.$x which can be used in the initializer or when accessing normally.


Declaration expressions

With declaration expressions you can declare a variable when calling a method with the out parameter. For example when parsing an int using the TryParse method you can declare the variable to out, when calling the method as such int.TryParse(s, out var x);


Await in catch/finally / Exception filters

You can now use await in catch/finally try … catch { await … } finally { await … } and as a new thing you can filter your exceptions with an if clause so it will only be caught if the terms are matched catch(E e) if (e.Count > 5) { … }

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 :)

Friday 4 April 2014

Roslyn .NET Compiler is now available as open-source

Yes, the new .NET compiler "Roslyn" is now available as open source. Exciting! Microsoft is also accepting contributions to the compiler. I can't even imagine where that could take it.

C# compiler source code available here.