EditorConfig: A simple way to manage consistent coding style

As per Robert Martin, Code should be elegant, efficient, readable like well-written prose . I’m also a strong believer of that. There are two key observations that I often see in code when multiple people work in a team. These are

  1. Everybody has different coding style so they write the code in their own way unless a common coding guideline is followed by team. It’s always advisable to make it part of an automated process.
  2. New members join the team and if they are not properly guided, chances are more to have spaghetti code. It becomes more problematic if the new members are junior developers/interns.

These issues can be solved at certain extent with the help of editorconfig file. In this file, we can define a set of rules (we will discuss few) which can be extremely useful to maintain the code consistency. This file can be made a part of solution so it is available to every team member.

Editor Config has it’s own format and guidelines to define the rules which can be seamlessly used by multiple editors like VS Code, Sublime, Vim, Netbeans, Eclipse, notepad++ and many more.

Follwing are the common set of rules which can be used

  • indent_style
  • indent_size
  • end_of_line
  • charset
  • trim_trailing_whitespace
  • insert_final_newline
  • tab_width

Let’s see an example. I have specified the editorconfig as

Here, I provided the indent style, indent size as tab and tab size. The last item trim_trailing_whitespace is set as true. One more thing, these rules will be applied to csharp (.cs) file but we can more file types as [.{cs,vb,js}] or even [*].

Note: The default values of these settings are available in Visual Studio IDE but once we add the editorconfig is added in project, it overrides the IDE setting. A notification also appears as

Let’s first discuss the first three as they are related. The default value for tab size is 4, but here I made it 3. As indent size is marked as tab, it has the power to resolve the whitespace vs tab issue :). Now when a developer uses whitespace for identation, it will turn to tab. In the below example, we can see, for few lines tab is used and for other spaces.

 

If the code is written with the above settings it will be as

Note – I have used ctrl+r, ctrl+w to see the tabs and spaces in the VS IDE.

Also the last settings helped in removing unnecessary white spaces after at the end of the line.

.NET related code conventions can be divided in three categories as

  1. Language Conventions
  2. Formatting Conventions
  3. Naming Conventions

Language Conventions: As the name suggests, these are related to the C#/VB language like using braces, using var instead of explicit type etc. The format looks as

options_name = false|true : none|suggestion|warning|error

Here we need to provide two values true/false and severity. true means prefer this style and false is opposite. Severity has four options as below

  1. none/silent : This will be used by code generation features only. No indication to user if it is not followed.
  2. suggestion : In case of violation, show a suggestion to user which appears as eclipses under the first two characters.
  3. warning : Shows an warning by underlining the variable with green squiggly.
  4. error: Shows an error by underlining the variable with red squiggly.

Let’s see an example. Here first we will see the editorconfig

Now let’s see the code

Here in first part of the image, we can see the gray squiggly and when we select, it displays a suggestion to change it var as per the editorconfig. Also we see the quick action icon which allows us to change with a preview (depicted in second part of the screen). Similarly, let’s see other options

 

In first part of the image, we can see a green squiggly as we configured to have braces and set is as warning. While second part of the pic, we see red squiggly which denotes an error as we configured to use predefined type. So based on our project needs, we can have a specific config file so we every member in the team follows the same rule.

Formatting Conventions : We have a set of rules which can help us in defining formatting guidelines of our code files. These formatting rules can be defined as

rule_name = false|true 

The rules could be as

Here first rule says system directive should be written first while other suggests not to have single line blocks, instead have it in multiple lines. There are many other rules which can be extremely helpful in maintaining the consistency. You can find all the list here. Similarly, Let’s have quick look on naming conventions.

Naming Conventions : Naming of the variable is also one of the key items while writing the code and .NET supports a list of rules which can be used in the editorconfig file. These rules are fully customizable. Let’s say we want a rule where we want public members as captilazied. It can look as

Here we created a new rule (public_members_must_be_capitalized) and provided the definition for that. For details, refer the documentation here.

Now we can see that this file is extremely useful. It may take some time to create but once done, pretty usefull. We can have files at solution and/or project level. At soultion we need to mention root = true as in the first image.

Editor Config has native support in Visual Studio 2017 but for earlier version of Visual Studio you can install the extension from here.

So hope you have enjoyed the post and would be able to use in your projects.

Cheers,
Brij

Using Deconstructors in C# 7.0

This is another awesome feature that got introduced with C# 7.0 which can be helpful in many scenarios while consuming various objects. Deconstruction is mostly discussed with another new feature System.ValueTuple but it can be used with other custom type as well. I have also discussed it in briefly in ValueTuple post at the link below. I will advise to go through that first before starting here.

How to use the new ValueTuples : A C# 7.0 feature

What is Deconstruction

It is a process of splitting an instance’s (value types and reference types) value into multiple parts and assigning them to new variables. Like, I mentioned that it is extremely useful with the Tuples which contains a set of values but can be used with other types.

How to use Deconstruction

We can leverage it in our custom classes, structs or interfaces. We need to implement Deconstruct method, which returns void and each deconstructed value should be added as out parameter in the method signature. We can have multiple overridden Deconstruct method based on the requirement. Let’s see an example

public class Person
{
    public string FirstName { get; set; }

    public string MiddleName { get; set; }

    public string LastName { get; set; }

    public int Age { get; set; }

    public Person(string fName, string mName, string lName, int age)
    {
        FirstName = fName;
        MiddleName = mName;
        LastName = lName;
        Age = age;
    }
    public void Deconstruct(out string fName, out string lName)
    {
        fName = FirstName;
        lName = LastName;
    }

    public void Deconstruct(out string fName, out string lName, out int age)
    {
        fName = FirstName;
        lName = LastName;
        age = Age;
    }
}

Here we can see that I have provided two Deconstruct methods, one with two out parameters: fName, lName and other with three : fName, lName, age.

Person p = new Person("Brij", "Bhushan", "Mishra", 32);

//(string firstName, string lastName) = p;
// OR
var (firstName, lastName) = p;

Console.WriteLine($"Person Details: First Name: {firstName}, Last Name: {lastName}");


(string firstName, string lastName, int age) = p;

Console.WriteLine($"Person Details: First Name: {firstName}, Last Name: {lastName}, Age: {age}");

Here I created the instance of Person with all the details. Then in first deconstruction, we got the first name and last name which calls the first Deconstruct method of the class which has two out parameters. Note that we have two ways of deconstruction. First we explicitly declare the type of each field inside parentheses (commented out) and another, using var keyword so that C# infers the type of each variable. Similarly later, second Deconstruct method is called which deconstructs it in three variables. It can be really useful when we have a class with numerous properties, fields and most of the time, we need only few there we can use the Deconstruct methods.

Similar to ValueTuples, we can use discards here as well. To discard a variable of Deconstruct method, we can use underscore (_) as

Person p = new Person("Brij", "Bhushan", "Mishra", 32);
(string firstName, _ , int age) = p;

Console.WriteLine($"Hello {firstName}, your age is {age}");

Deconstruction use Extension method

There could be scenarios where you want to this feature but its an existing class where changes are not possible (like some third party classes, classes own by different team or even framework classes), there we can add this as an extension method. Lets assume that Person class doesn’t have Deconstruct methods and we can add it as an extension method as

public static class DeconstructionExtensions
{
    public static void Deconstruct( this Person p, out string fName, out string lName, out int age)
    {
        fName = p.FirstName;
        lName = p.LastName;
        age = p.Age;
    }
}

It will work in the same way as earlier.

Hope you have enjoyed the post and will be able to use in your day to day coding.

Thanks
Brij

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.

Cheers,
Brij

TransactionScope: A simple way to handle transactions in .NET

Have you ever tried implementing transactions using C# code? Normally, we implement transactions in SQL where multiple Insert/Update statements takes part in it. A Transaction follows the ACID (Atomicity, Consistency, Isolation, Durability) rule where either all the statements get committed or all get canceled and rolled back. TransactionScope allows us to implement it at application level. There could be some scenarios where you are required to do different operations in the same database or even multiple databases (distributed transaction) or due to some other constraints, it cannot be done at database level. It is also very helpful for application developers if they have less exposure to database.

What is TransactionScope

TransactionScope got introduced with .NET 2.0 as part of  System.Transaction. It is a class which provides a simple way to make a set of operations as part of a transaction without worrying about the complexity behind the scene. If any of the operation fails in between, entire transaction would fail and rolled back  which undo all the operation that got completed. All this would be taken care by the framework, ensuring the data consistency.

How to use the TransactionScope?

To use this, you need to add the reference of System.Transactions reference which is part of framework libraries (normally it wont be added by default). Once it get added, add the namespace System.Transactions wherever we want to use this. The syntax would look as

try
{
    using (TransactionScope scope = new TransactionScope())
    {
        // Do Operation 1
        // Do Operation 2
        //...

        // if all the coperations complete successfully, this would be called and commit the trabsaction. 
        // In case of an exception, it wont be called and transaction is rolled back
        scope.Complete();
    }
}
catch (ThreadAbortException ex)
{
    // Handle exception
}

Here we can see that we have used Disposable block while creating instance of TransactionScope, it makes sure the dispose gets called when it gets out of the block and ends the transaction scope.

In one Transaction scope, we can do multiple operation connecting to different databases as

using (TransactionScope scope = new TransactionScope())
{
    using (con = new SqlConnection(conString1))
    {
        con.Open();

        // Do Operation 1
        // Do Operation 2
        //...
    }

    using (con = new SqlConnection(conString2))
    {
        con.Open();

        // Do Operation 1
        // Do Operation 2
        //...

    }

    scope.Complete();
}

Here we are using two connection strings to connection different databases. We can use as many based on our requirement. We can have nested transactions as well. It could be as

public void DoMultipleTransaction()
{       
    try
    {
        using (TransactionScope scope = new TransactionScope())
        {
            using (con = new SqlConnection(conString1))
            {
                con.Open();
                // Do Operation 1
            }

            OtherTransaction();
            scope.Complete();
        }
    }
    catch (ThreadAbortException ex)
    {
        // Handle exception
    }
}

private void OtherTransaction()
{
    using (TransactionScope scope = new TransactionScope())
    {
        using (con = new SqlConnection(conString2))
        {
            con.Open();
            // Do Operations
        }
        scope.Complete();
    }
}

Here the outermost transaction is called as rootscope and here even if the inner transaction (OtherTransaction above) gets completed by calling scope.Complete(), if the rootscope complete could not be called due to various reasons, then the complete transaction would be rolled back including inner transactions.

Note: You might get one of the following exception while executing distributed trsanctions

  1. MSDTC on server is unavailable
  2. Network access for Distributed Transaction Manager (MSDTC) has been disabled.

 

Both the error are due to the same reason, first one occurs when you have the database and the application the same server while 2 if on the other server. For same server, go to run-> cmd-> services.msc. Run the service named Distributed Transaction Coordinator and make the startup type automatic so that it gets started again in case of system restart. For 2, follow the link to cofigure MSDTC.

TransactionScope provides various TransactionScopeOptions which defines transactions behavior for the scope. Lets see an example

using (TransactionScope scope = new TransactionScope())
{
    // Do Operation
    using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.Required))
    {
        // Do Operation
        scope1.Complete();
    }
    using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        // Do Operation
        scope2.Complete();
    }
    using (TransactionScope scope3 = new TransactionScope(TransactionScopeOption.Suppress))
    {
        // Do Operation
        scope3.Complete();
    }

    scope.Complete();
}

Here we created three transactions under the parent transaction with different TransactionScopeOptions. By default the scope is required, which applies to parent transaction here. It is a rootscope which creates a new transaction, and mark it as an ambient transaction. scope1 is also created with required and as we have already an ambient transaction (scope) so it joins the parent transaction. scope2 got created with option as RequiresNew which means it is a new transaction which is independently works with ambient transaction. scope3 got created with suppress option, which means it doesn’t take part in any ambient transactions. It gets executed regardless whether ambient transaction executes successfully or not. All the ambient transactions gets committed once the parent (global) scope completes.

Hope you enjoyed this post and will be using transaction in your future requirements.

Cheers,
Brij

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
~Person()
{
    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.

Cheers
Brij

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.

Usages

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.

Limitations

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.

Cheers
Brij

Ref and Out improvements in C# 7.0

If you have been observing last few releases of C#, the key focus of C# language team is to improve the developer productivity by reducing the repetitive/common code and performance of the application. First one, helps a lot in writing cleaner and concise code so it mainly contains logic instead of number of lines for null check etc.

ref and out keywords are available with C# since long and very useful in many scenarios. These were serving the purpose quite beautifully but there were few improvements in C# 7.0 which makes it more attractive. In this post, I am not going to discuss the basics of these keywords detail but mainly focus on the improvements in C# 7.0.

Ref keyword – This keyword is used to pass the parameter by reference (even if it is value type) to a method. When we pass the parameter by reference, any change that takes place in the called method also reflects in calling method. ref keyword should be used by in calling method while passing the parameter and in the called method as well. Also unassigned variable cannot be passed as well. Lets see a quick example

static void Main(string[] args)
{
    Point p;
    p.x = 10;
    p.y = 20;

    UpdateCoordinates(ref p);

    Console.WriteLine($"Point coordinates x : {p.x}, y : {p.y}"); // Output: Point coordinates x : 20, y : 40

    Console.ReadKey();
}

static void UpdateCoordinates(ref Point p)
{
    p.x = 20;
    p.y = 40;
}

public struct Point
{
    public int x, y;
}

Here Point is a value type and it is passed using ref keyword. Here the variable gets updated in the called method and it shows 20 and 40 in output.

Note: ref keyword can be used with reference types as well. You can refer one of my previous posts for details here 

C# 7.0 Enhancements :

As in the example above, the values are passed by reference in the method but what about returning the values by reference. For this, there were two enhancements introduced

1- Ref Returns : For this, we need to add ref keyword after return statement in a method and before the return type in the method signature. This feature can be used only in Class (not structs). Let see the Point class as

public class Point
{
    private int x, y;

    public Point(int a, int b)
    {
        this.x = a;
        this.y = b;
    }
    public ref int GetX()
    {
        return ref this.x;
    }

    public ref int GetY()
    {
        return ref this.y;
    }
      
    public void Display()
    {
        Console.WriteLine($"Point coordinates x : {x}, y : {y}");
    }
}

Here I added two methods GetX and GetY which returns x and y by reference. ref keyword also got added with method signature as well.

2- Ref Locals: To store a reference variable (ref), returned by a method, the local variable should also be added with ref keyword before the type and adding the ref keyword before the method call. Now the local variable contains the value by reference and has the ability to change the value of variable which will reflect in the original variable as well. We can call the method without using the ref keyword which will behave as normal method call and the returned value will be a copy of the value.

static void Main(string[] args)
{
    Point p = new Point(10, 20);

    ref int i = ref p.GetX();
    i = 50;

    p.Display(); // Output: Point coordinates x : 50, y : 20

    int j = p.GetY();
    j = 100;

    p.Display(); // Output: Point coordinates x : 50, y : 20

    Console.ReadKey();
}

Initially the values of x and y are 10 and 20. We got the variable x by refernce and updated it to 50 using variable i which is reflected in output. We also received the value of y without using ref which creates a copy of the variable and updating it doesn’t affect the instance p.

It is a really nice feature in scenarios where the values are required to be updated at different places. This also can be when we have a collection of values and few of the items can be updated by the caller method based on the need.

Enhancements in Out parameter: There is a small enhancements in out parameter usage. Earlier out parameter is used as

static void Main(string[] args)
{
    int age;
    string name = GetUserDetails(out age);
    Console.WriteLine($"name : {name}, age : {age}");

    Console.ReadKey();
}

public static string GetUserDetails(out int age)
{
    age = 32;
    return "Brij";
}

Now after enhancement, we dont need to declare the variable first, instead we can direcly declare in the paarmeter as well as

static void Main(string[] args)
{
    //int age;
    string name = GetUserDetails(out int age);
    Console.WriteLine($"name : {name}, age : {age}");

    Console.ReadKey();
}

Although this is a small enhancement but makes more sense.

Hope you enjoyed the post and will be able leverage these enhancements in your day to day coding.

Cheers,
Brij

How to use the new ValueTuples : A C# 7.0 feature

Tuples are one of the awesome features of C# which was initially introduced with .NET 4.0. Although it is less used but it become very handy when we need to return multiple values from a function. We have another option to use out operator but it is not recommended due to various reasons. I am not going to discuss it in this post. Tuple is a static class under namespace System which implements a Factory pattern to create the instances of Tuples. It has a Create method with eight overloads which allows us to have 8 elements in a Tuple. Also, we can have nested tuples as well.

There were few issues with System.Tuple. It is a reference type so even if you are returning few values type elements , the instance gets created on heap, means adding pressure on Garbage Collector. It doesn’t allow to provide a name to the elements, instead we have to refer it as item1, item2.. etc. Also, the way we used to create Tuple is also quite verbose.

Tuple in C# 7.0

C# 7.0 introduces System.ValueTuple as a new language feature, which resolves the problems mentioned above and provides few more additional features. And Yes, it has the power of Value Type so it removes the GC overhead of the old tuple.

Note: ValueTuple is available as indepedent nuget package. You can install it from here.

Lets see few examples of new Value Tuples,

Tuple Literals

Tuple literal is a comma separated list of literal (of various types), surrounded by parenthesis.

static void Main(string[] args)
{
    // Tuple Literals
    var tpl = (1, 2);
    var author = ("Brij", 32, "https://codewala.net/");
}

public (string, int, string) Author { get; set; } = ("Brij", 32, "https://codewala.net/");

Here we can see that creating Tuple is now a simple assignment operation. Also we can have different types in a Tuple which helps in combining various set of information in a tuple based on the need.

Using Tuples as Return Type

We can use value tuple as return type and the syntax looks so simple as


var author = GetAuthor();

Console.WriteLine($"Author Details: Name: {author.Item1}, Age: {author.Item2}, BlogURL : {author.Item3 ?? "URL not available" } ");

private static (string, int, string) GetAuthor()
{
    string name = "Amit Kumar";
    int age = 33;
    return (name, age, null);
}

Naming the Tuple Elements:
Yes, we can name the tuple elements and this also provides the intellisense support. This is one of the best improvements. We can write the above code as

var author = GetAuthor();
WriteLine($"Author Details: Name: {author.name}, Age: {author.age}, BlogURL : {author.url ?? "URL not available" } ");

private static (string name, int age, string url) GetAuthor()
{
    string name = "Amit Kumar";
    int age = 33;
    return (name, age, null);
}

We can give names in tuple literal as well

var author = (name: "Brij", age: 32, url: "https://codewala.net/");      
Console.WriteLine($"Author Details: Name: {author.name}, Age: {author.age}, BlogURL : {author.url ?? "URL not available" } ");
// Or we can also write as
(string name, int age, string url) author = ("Brij", 32, "https://codewala.net/");

Let’s move to another exciting feature

Value Tuple Deconstruction

Value tuple allows us deconstruct the tuple and access the elements as a local variable. We need to assign a tuple to variable surrounded by parenthesis.  Broadly, it can be done in three ways

  • Providing the types of each element in parenthesis.
  • Using var keyword outside the parenthesis (it is applied to each variable).
  • Or using existing variables

Lets see the examples


// First Option - Providing the type of each element. var is also allowed.
(string authorname, int age, var blog) = GetAuthor();
Console.WriteLine($"Author Details: Name: {authorname}, Age: {age}, BlogURL : {blog ?? "URL not available" } ");

// Second Option - Using var outside of the parenthesis
var (authorname, age, blog) = GetAuthor();
Console.WriteLine($"Author Details: Name: {authorname}, Age: {age}, BlogURL : {blog ?? "URL not available" } ");

// Third option - Using existing variables
string authorname = "Brij";
int age = 32;
string blog = "https://codewala.net/";

(authorname, age, blog) = GetAuthor();
Console.WriteLine($"Author Details: Name: {authorname}, Age: {age}, BlogURL : {blog ?? "URL not available" } ");			

Now it may happen that you are not sure about all the elements or want to discard few , you can use underscore (_) as

var (authorname, age, _) = GetAuthor();
Console.WriteLine($"Author Details: Name: {authorname}, Age: {age}");

Last thing, as we have now System.Tuple and System.ValueTuple both, ValueTuple provide a nice extension method as ToTuple() to convert it to System.Tuple.

C# 7.1 Enhancements: 

There are a small enhancement in C# 7.1 which can save few keystrokes as now elements names are inferred from the variables. Let’s see the example

// Earlier
string name = "Brij";
int age = 32;
string blog = "https://codewala.net/";
var author = (name: name, age: age, blog: blog);

// With C# 7.1 
string name = "Brij";
int age = 32;
string blog = "https://codewala.net/";
var author = (name, age, blog); // Element names would be - name, age, blog

Hope you have enjoyed this post and will be able to use in your day to day coding.

Cheers,
Brij

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.

Thanks
Brij

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.

Cheers
Brij