Category Archives: C#

New in C# 7.0: Part 4 – Expression-Bodied Members

This week, we’re discussing Expression-Bodied Members in C# 7.0. Now, this was an existing feature in C# 6.0, but it was limited to only methods, like so:

With the new additions in C# 7.0, we can now do Constructors, Deconstructors, and Properties. A Constructor definition looks like this:

Similarly, a Deconstructor looks like this:

Finally, although auto-properties are shorter, you can write properties with specific implementations with this new shorthand:

That’s it for this week. Next week, we’ll go over Pattern Matching.

New in C# 7.0: Part 3 – Ref Returns

Picking up where I left off last week, there’s another interesting new feature called “Ref Returns”. Similar to Ref Locals, this allows you to return a reference from a method, and then optionally use it as a Ref Local in the calling code.

Let’s look at the following method definition:

The method takes an integer, and returns an entry from an array with that index. The important differences from a normal method is the ref keyword in the method signature, and the ref keyword after the return keyword. This tells the compiler that you want the method to return a reference to the variable/object, instead of passing it back by value.

When calling the method, we do this, defining a Ref Local in the process:

Or, if we don’t want to use it as a reference, and just want to use the value, we can omit the ref keyword in both places.

That’s it for this week. Next week, we’ll be talking about Expression-Bodied Members.

New in C# 7.0: Part 2 – Ref Locals

After about a year, I’m finally trying to start writing some more blog posts, so I’ll pick up right where I left off. Today, I’m going to give a brief overview of Ref Locals.

Ref Locals allow you to create an alias for a variable so that you are not creating another copy of the data or reference (in the case of an object), in memory. In previous versions of C#, if you wanted to refer to a local variable with a different name, you’d have to do this:

This unfortunately creates another copy of the variable, so changes to the original are not reflected in the copy. With Ref Locals, you can ensure changes are reflected in both places:

This is particularly helpful when dealing with arrays of items.

The changes will be reflected in the array itself, instead of just in the copy version of the variable.

That’s it for today. Next week is Ref Returns.

New in C# 7.0: Part 1 – Out Variables

This is the first post in a series on the new features available in Visual Studio 2017 and C# 7.0. Today, I’m going to be talking about Out Variables.

One of the great new features is probably something you will use frequently, which is defining an out variable, and using that variable in the same statement. Currently, to use an out variable, one must do something like this:

With simple syntactic sugar in the new version, you can instead do this:

That’s pretty much it. This is definitely a small, but useful little addition to the C# language.

Next time, I’ll be going over Ref Returns and Ref Locals.