C# 6.0 and 7.x posts links

Hello All,

There are rapid changes to C# language recently. Although these are very useful in writing cleaner, concise, less repetitive and performance oriented code, I see very less usage of these in our day to day coding. So I thought of writing these features as blog posts to learn myself and share with others. In this post I am just listing down the links of my earlier C#6.0 and C# 7.x blog posts so that it is easily available. Do let me know if there is any important feature that I missed which could be useful in our day to day coding.

C# 6.0 features

C# 7.X features

Hope you’ll enjoy these posts and able to use these in your day to day development.



Expression-bodied Members in C#

Expression-bodied members are one of the shiny features of C# 6.0 which allows us to write the implementation of a member in more readable and concise format. We can use the expressions as a definition of the members instead of using the statement block. The format of the expression is as

var=> expression

This is also called lambda expressions and => is called lambda operator. In C# 6.0, we can use this feature with methods and properties. Lets see few examples

Properties: Instead of using the normal getter, we can use expressions with lambda operators as mentioned.

// Normal way
public string FullName
    get { return string.Format("{0} {1}", FirstName, LastName); }

// Using Expression (C# 6.0)
public string FullName => string.Format("{0} {1}", FirstName, LastName);

// Above code can be more consize using string interpolation
public string FullName =>$"{FirstName} {LastName}";

If you are not aware of String Interpolation, you can refer one of my previous post here.

Methods : Similar to properties, C# 6.0 also allows us to use expressions while writing methods which returns values to the caller.

// Normal way
public string GetFullName(string firstname, string middleName, string lastName)
    return middleName == null ? $"{firstname} {lastName}" : $"{firstname} {middleName} {lastName}";

// Using Expessions 
public string GetFullName(string firstname, string middleName, string lastName) => middleName == null ? 
             $"{firstname} {lastName}" : $"{firstname} {middleName} {lastName}";

Limitation: There is a limitation of expression bodied members.We can have just single statement in the expression and statement blocks {} are not allowed. In my above example, I used the ternary operator conditional logic instead of using if statement block.

As we can see C# 6.0 added the capability for methods and read only properties, C# 7.0 added more power to this feature and now we can use expressions in constructors, set accessors in properties, indexers and finalizers. Let’s take a look

Properties: We saw earlier enhancements for Property which was similar to get accessor

private string _name;
public string Name {
    get => _name;
    set => _name = value;

Here this is a very simple example and in this scenario, I would like to prefer the Auto propery as below

public string Name { get; set; }

However in my earlier example where I was returning FullName after concatenating two strings, there it is better suited. So when you are doing some smaller operations as well in your property then it is better choice.

Constructor: We can leverage the expression with the constructors as well. Let’s see

// Earlier
public Person(string name)
    this.Name = name;

// With C# 7.0
public Person(string name) => this.Name = name; 

As mentioned earlier that statement block can’t be used here. So say if I have multiple parameters to pass so I can use another C# 7.0 features: ValueTuple and Destructors.

public Person(string firstName, string lastName) => (FirstName, LastName) = (firstName, lastName);

I wrote some time back on ValueTuples and Deconstructors, you can take a look.

Finalizers: We can use expressions in finalizers as well. Let’s see

// Earlier
    Console.WriteLine("Person's destructor");

// Using Expressions
~Person() => Console.WriteLine("Person's destructor");

There are many new features that got intrudocuced in C# 6.0 and C# 7.X (7.0, 7.1, 7.2) if these are used together, can provide lot of value. The whole idea is write cleaner, concise and more performant code so that the focus is more on business logic.

Hope you are enjoying the enhancements.


Span: A new upcoming feature of C#

With the ground-up changes in ASP.NET with ASP.NET Core which is still going on, it appears that now it is turn of C# language and the run-time. I am sure the ground work must have been going on from last couple of years as these changes required massive ground up changes but I am hoping there are lot more changes will take place in future. After using the power of Span<T> and Memory<T> in ASP.NET Core 2.1, which is claimed to be 70-80% faster than ASP.NET Core 1.X, it is planned to release for all of us. In this post, we will be discussing the new type Span<T>. Before jumping directly into Span<T>, let’s have a quick overview current types.

Every object in C# can be categorized in two type: ValueType and Reference Type. We know Value Type instances gets created on stack and only available in the current scope while reference gets created on managed heap which is taken care by Garbage Collector (GC). Garbage collection is expensive process so if we can minimize the reference type object creation and use more Value Type objects then it could be a huge performance benefit. I am not saying that GC is not properly optimized or similar but no GC is always better that any performance optimized GC.  Let’s understand how Span can help in few scenarios.

Note – Currently this feature is available in prerelease state so some changes in final release are expected. To use this this feature, we need we need Visual Studio 2017 15.5+ version and need to install nuget package – System.Memory (Check the include prerelease checkbox while searching it). You can have a look here if getting an error.

What is Span<T>

Span<T> is a ValueType which allows us write low level code in safe and efficient way without any GC overhead. It’s as efficient as working with unmanaged pointers with the benefits of C#. Span<T> represents a contiguous region of arbitrary memory which could be unmanaged memory, memory allocated on stack and arrays or strings.

Let’s understand it with the help of a string.

Here we can see that when we use Substring method on a string, a new copy gets created on the heap. But, we may not want a new copy as we just read that particular part and even sometimes we may need to update the original string itself. Let’s see how does Span<T> work here

So here we can see that we got a ReadOnlySpan from the string and then used Slice (similar to Substring) method of span to get a specific part of the string but here both the variables refer to same memory location. As span is a struct, lastName doesn’t cost any GC overhead. The two parameters of the Slice method are, starting index and length of the sliced part. Let’s see the complete example

Span<Char> name = "Brij Mishra".ToCharArray().AsSpan();

Span<char> lastName = name.Slice(5,4);

lastName[3] = 'k';

Console.WriteLine(lastName.ToString()); // Prints -> Miskra
Console.WriteLine(name.ToString()); // Prints -> Brij Miskra

lastName[5] = 'k'; // Throws Exception -> System.IndexOutOfRangeException'

Here we can see that change in one character using lastName reflects in both the variables. In the last line, when we try to access the fifth character via lastName, it throws IndexOutofRangeException even we know that we have character at this location. But since lastName was created with only four characters, we cannot access it.

Span vs ReadOnlySpan

As the name suggests Span<T> allows us update the values but many a times, we use the sub-string only for readonly purposes. Say we have a method where we need to pass the part of the string which is just needed for reading purposes, it is safer to pass ReadOnlySpan to save from any accidental update because it will affect all the places inside the program. The current version of the package, returns the ReadOnlySpan while getting the span out of string. So if you are sure that we need the span for read only purposes then we should use ReadOnlySpan. Trying to make any changes in ReadOnlySpan will be an compile time error.


If you are working with payloads where you create and manipulate strings a lot, it can be quite useful. Similarly, parsing needs usage Substring where it can be quite useful. Other usages are not so common like working with buffers and doing multiple transformations like encoding/decoding, parsing, escaping etc. Working with network buffers can be other quite useful scenario.


Span<T> is designed to  stay in the stack onlu which is a limiting factor as well. Few key limitations are

  • We can’t box/unbox because it can’t stay on the heap
  • We can’t have it as fields in classes or even in non-ref structs
  • Cannot be used in lambda method because it might turn into field.
  • Due to stack only field, cannot be use in async methods or iterators

Even having quite many limitations, it can be really helpful where you have big structs, file I/O , Network I/O, string manipulations, working with buffers, synchronous methods.

Hope you have enjoyed the post.


Error: CS0619 Span is obsolete

Recently I was exploring various new features of C# 7.x and recently I thought of looking into one cool feature Span but faced hard time to start. So to start with it, one need to install the nuget package System.Memory as

Make sure, you have Include Prerelease check box selected. After that we need to include System.Memory namespace. Once I wrote my first variable as

Upon building the project, I got the following error

I thought something as new as span, how come this be obsolete. I spent some time googling it with no specific result.
I looked my Visual Studio version which Visual Studio Enterprise 2017 v 15.4.2. I thought to update the same as there were few minor releases after that. After updating it to the latest available v15.6.2, I tried again and this time no error.

Initial error was not intuitive enough and updating Visual Studio, i consider last thing unless it is mentioned somewhere.

If you facing similar issue, this may be helpful to you as well.


Read-Only Structs: C# 7.2

C# supports two basic types : Value Type and Reference Type. The main difference between them where they get created: Value Types gets created in stack while Reference type in Heap. Other key difference, for ValueType whenever a parameter is passed as an argument, a copy gets created and passed while for reference types, refernce of the same onstance gets passed. There is another type called – Nullable Type but actually it’s a wrapper over value type. I wrote a post on it, you can have a look here.

All the Value Types internally inherits System.ValueType. Struct is also a ValueType which inherits same.

It’s a good idea to make struct immutable, it means once it is initalized, it cannot be modified. System.DateTime is an immutable Value Type. It provides many static methods but it always returns a new instance.

To make a struct immutable, the simple way to make all the members readonly and initializ these values via parameterized constructor. It’s members should be exposed via getter fields.

Let’s see an example

public struct Person
    public string FirstName { get; }

    public string LastName { get; }

    public int Age { get; }

    public Person(string firstName, string lastName, int age)
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Age = age;

Above struct is immutable as there is no way to modify the initialized variable.

Read-Only structs:

Prior to C# 7.2, to create an Immutable value type, we had to write the struct in a way so that it doesn’t allow an update as in above example. If few fields are left without marking readonly/const unknowingly then it wont work as expected. C# 7.2 allows us to add readonly modifier before struct which makes sure thatg all the members are read only. If any member doesn’t, it will throw an error. Above struct can be marked as read only as

public readonly struct Person
    public string FirstName { get; }

    public string LastName { get; }

    public int Age { get; }

    public Person(string firstName, string lastName, int age)
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Age = age;


One important thing, the parameterized constructor should be used to create the instance always else in case of using parameterless constructor use, the member would be set to default value which cannot be ever changed and we probably won’t want this. We cannot make parameter less private for struct in C# and even if there is a way we can bypass that. We can have some validate method which should be fired before using that struct’s memebers. I wont discuss this in details here.

Read-only struct provides framework support to write readonly struct and help us to avoid any unintended modification. In my previous post, I discussed about In modifier, it also can be used while passing any immutable object. These changes like ref, out, in etc in .NET framework got developed to make low level programming simpler and faster. Few more changes, we will be discussing in coming posts.

Note: If you are not sure how to use X# 7.2 (7.x) in your project, you may have a look on one of my previos post here.

Hope you enjoyed the post.


Using In Parameter Modifier : C# 7.2

Changes are the only constant thing in the world and that got little faster with C# new releases as we have minor now releases (also referred as point releases) with significant enhancements. New features are getting added and existing features are getting enhanced. In one my earlier posts, I discussed about Ref and Out improvements that took places in C# 7.0. You can go through the link below.

Ref and Out improvements in C# 7.0

Let’s have a quick look on it

Here we can see that if we want to pass the argument by ref then reference of the instance (value type or reference type) is passed and any change the in the argument in the called method reflects in the calling method as well.

Note – If you are curious about the using the ref keyword with reference type object, you can have a look to one of my previous posts by clicking here.

Out parameters, are also like pass by reference except it enforces a rule that calling method pass the variable without initializing while called method must initialize.

Say you have a big struct or any other value type, that you have to pass as an argument. Everytime you will call the method, a new copy will be created and passed to the method. If this has to be done multiple times then it can be a major performance issue.

Also, out keyword  forces to the called method to initialize the variable but we dont have opposite option. And ref keyword allows to pass the paramter via reference and here caller and called method both can modify the variable.

C# 7.2 introduced another keyword In which provides the ability to pass the argument as readonly. It fails at compile time if there is any code which tries to modify in the called method. The variable must be initialized before passing as a parameter.

Lets see an example

There are few restrictions.

  1. In, Out and Ref keywords cannot be used in async methods.
  2. Cannot be used in iterator methods (which has yield statements)
  3. Marking the arguments of main method as In, makes it invalid as entry method.

To summarize, In keyword can be really useful when you are passing a big value type object to method which is called multiple times because instead of creating a new copy in each call, it will be passed by reference.


How to use C# 7.2 (7.x – minor releases) in your Project

C# 7.0 and other minor releases (7.x) are loaded with many awesome features and I’ve been all those nice features recently. You can have a look here. I have seen many people facing issues with using these features. So in this quick post, I will discuss how you can leverage C# 7.x features.

Using Visual Studio 2017

To use C# 7.x features, Microsoft recommends to upgrade the IDE to Visual Studio 2017. To use C# latest minor releases C# 7.X (currently C# 7.2), this is not enough, we need to follow the below steps

  1. Right click on the project => Select Properties
  2. Select Build tab from the left pane.
  3. Go down and select Advanced button
  4. Select the Language version as

Here we can see tha the default selection is C# latest major version (default) which means 7.0. We also have the option to select specific versions like 7.0/7.1 but for 7.2, we need to select C# latest minor version (latest).

Using Visual Studio 2015

As mentioned above that Microsoft recommends to use Visual Studio 2017 for C# 7.x features but as these features are implemented by compiler so as long as your build is pointing to correct version of CSC, you can build the new features. However, Visual Studio 2015 may not like it as it doesn’t understand the new features and you may not get intellisense or any other IDE support.

To use C# 7.X in Visual Studio 2015, install the following nuget package


Once, you install this package, you should be able to use the latest language feature in VS 2015. However you might red   squiggly in the editor as

Here we can see that even the VS2015 editor is complaining but it is getting built.

Using System.ValueTuple

ValueTuple is not available by default as part of C# 7.0, we need to install following nuget package


Hope you like the post and able to use the C# latest features.