Conditional Ref, Ref ReadOnly, Ref Reassignment in C# 7.X

C# has gone through the major changes starting from 6.0 specifically from 7.0. I wrote a list of posts on C# new features which covers key topics that can be used in our day to day programming. You can find the previous posts list below

C# 7.X posts

Today we are going to discuss few uses of ref which provided altogether different meaning to it.

Conditional Ref: This feature was introduced in C# 7.2. Before going to this specific topic, we know that ref keyword has been in C# prior to 7.X and we used this keyword to pass the values by reference as

        static void Main(string[] args)
            TestRef testRef = new TestRef() { Description = "First Object" };
            double price = 20;

            DoSomething(ref price, ref testRef);


        public static void DoSomething(ref double finalPrice, ref TestRef test )
            finalPrice = finalPrice * 1.2;
            test = new TestRef() { Description = "Second Object" };

   public class TestRef
        public string Description { get; set; }

Here I am passing a value type variable price and an object of a class type TestRef (we know classes are reference type). So let’s see the result before discussing it

Here we can see that  finalPrice is passed as ref parameter and when we are updating the value in the method, the updated value is available in caller function as it was passed using ref. Similarly in case of TestRef’s instance as ref parameter, a new instance is assigned to the variable then the variable in caller method also got updated with the new instance. It is because the instance testRef was pass passed using ref.

We have discussed few other usages of ref keyword which got introduced in 7.X, conditional ref are one of the interesting ones. We used ternary operator in past as

    var smallArray = new int[] { 1, 2, 3, 4, 5 };
    var largeArray = new int[] { 10, 20, 30, 40, 50 };

    int index = 7;
    int val = ((index < 5) ? smallArray[index] : largeArray[index - 5]);
    val = 0;

Here val gets assigned with value 0. If we change this value, no update takes place in the original array. Now let’s see the power of ref keyword here. We can see that based on condition first or second value is returned but now we can use ref keyword as

    ref int val = ref ((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]);
    val = 0;

Here I am using the same array but here we are using the ref keyword in Consequent (smallArray), Alternative (largeArray) and put the ref at the whole ternary expression. Also, at left side added ref as ref int val. Removing any ref keyword will produce an error. If we want to store it as normal value then we may remove ref from left and outer ref from right.

Now val is pointing to the largeArray[2] and as we assigned it to 0, array also got updated.

Also as these ternary operator refers to a memory location so we may use it as LValue as

    ref ((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]) = 0;

ref readonly: I discussed about ref keyword in one of my previous posts (refer here). It allows us to pass the values by reference, return the value by reference. But what about if we want to return a value which we dont want to get modified by the caller. Either we pass it by value (which creates a copy ) or we can use ref readonly. It will make sure the reference is returned (not the copy) but caller cannot modify the returned value. Let’s see an example

static void Main(string[] args)
    var points = new Point[] { new Point(1,2), new Point(1, 2), new Point(1, 2) };

    var point = new Point();

    ref readonly var myPoint = ref point.GetPoint(2);

    // It is a compile time error
    myPoint.Y = 10;

struct Point
        public int X;
        public int Y;

        public Point(int x, int y)
            X = x;
            Y = y;
            points = null;

        private Point[] points;

        public void SetPoints(Point[] arrPoint)
            points = arrPoint;

        public ref readonly Point GetPoint(int index)
            if (points.Length &amp;gt; index)
                return ref points[index];
                throw new KeyNotFoundException();

Here you can see GetPoint method in struct which returns one of the points from the array using ref keyword. Now if we want that the caller should not be able to modify it, then we need to put ref readonly in return type (not while returning). Now if you see while calling, we have to use ref on the right side and ref readonly on the left side and changing that variable would be a compile time error. As the method return ref readonly we cannot remove readonly from left side, however we can completely remove ref from both side including readonly then it will create a copy of the point.

If you remember In (about In)operator which allows the variable pass by reference but called method can’t modify that, it is opposite as Caller cannot modify the returned variable however the keywords are bit different.

As I used struct for the example which is value type and using operators like ref, In etc makes sure that we dont create a copy of that. However we know value type are easy to initialize and destroy in memory but Microsoft recommends that if the size of the struct is more than System.IntPtr.Size then we should avoid creating the copy of the struct.

Tip: Use the ref readonly for large structures and top preserve the immutability of the data structure.

Ref ReAssignment – This feature was added in C# 7.3 which allows us reassign a ref local variable to different location and obviously that should be of same type. Lets see an example

In this example I am going to use earlier Point class and just removed readonly from GetPoint method.

Here we can see a C# 7.2 feature where we get the reference of an array via ref and if we update the ref variable via another ref instance, it updates the original array as we can see in the tool tip.

Ref reassignment allows us to update ref local variables to different location which was not possible prior to C# 7.3 as

Here we can see refPoint was referencing to the last element of the array and once we assign to the first element, it started referring to the first object. This code will work only C# version is selected as 7.3.

As mentioned earlier, C# 7.X and 8.0 has lots of new features and few important ones we discussed earlier. In this post, we discussed three important features related to ref- Conditional ref, ref readonly, ref reassignment. Hope you have enjoyed the post.




Hosting ASP.NET Core Applications on IIS : In-process hosting

In my previous blog post, I discussed a brief history about ASP.NET Webforms/MVC applications and its deployment to IIS. Then we took a look in the new framework ASP.NET Core which was introduced keeping in mind the performance and cross platform readiness. As performance was one of the key drivers, they introduced entity framework core and all thenew web server named Kestrel. You can read the blog post here.

Hosting ASP.NET Core Applications on IIS – A Detailed Look

We discussed about ASP.NET MVC Core 2.1 and discussed some internal details about its deployment on IIS as a reverse proxy (which is recommended), and also took a look on using Kestrel as an Edge Server. Although Kestrel is matured enough to be used as an Edge Server but still IIS is considered better option. We also saw, how ASP.NET Core requests are handled by IIS. We need to install the .NET Core Hosting bundle (download here) which adds a module named ASP.NET Core Module  (ANCM). ANCM is responsible to route the core request to Kestrel.

With ASP.NET Core 2.2, Microsoft introduced In-process hosting. This model allows us to host the core 2.2 directly inside the worker process (w3wp.exe) which is similar to earlier ASP.NET version. Let’s take a pictorial view

We can see that there is no dotnet.exe is involved here in the second part. All the components ANCM, CoreCLR and application code are loaded in same worker process.

To use the latest feature, we need to install the latest 2.2 bundle (download here) which installs the upgraded version of ANCM also referred as ANCMv2. After installation, both the modules can be seen in IIS modules section as

Why new version of ASP.NET Core Module (ANCMv2)?

Earlier the idea with ANCM to use IIS as a reverse proxy and leverage Kestrel as a backend web server (as it was not hardened enough as an edge server) but as Kestrel got all the required enhancements, MS reworked on ANCM so that IIS can be used another platform to host core applications without the need of Kestrel. ANCM got redesigned and divided in two components, a shim and a request handler.

     Shim – As the name suggests, it is a very light weight component which is continue to be installed as a global module via bundle which just work as an interface between IIS and request handler.

   Request Handler – Request Handler is now an independent component which does all the work and can be updated via nuget. We can have multiple versions of request handler side by side. It means we can have multiple application using its own request handler.

With earlier ANCM, it was available as global singleton module which is shared by all the application which is a major bottleneck in releasing newer versions as it has to support every application. With the new architecture, we also get better process management, performance enhancements and easy updates via nuget.

We have so many benefits with the new model however we have one limitation – one application pool can only host only one application (In ASP.NET Web Form/MVC we could share app pools with multiple applications) as we don’t have the concept of Application domains in CoreCLR and this feature supports to CoreCLR only.

Let’s see an example

Now I have created another sample web application application using ASP.NET Core 2.2 (used VS 2017 v15.9.4) and deployed to IIS after publishing that.

There is no brainer here, let’s see the processes

Just to compare with earlier version I am adding both here.

So we can see the difference, in first scenario (<ASP.NET Core 2.2) the application is running under dotnet.exe while in second scenario, it is running under the worker process (w3wp.exe) itself which boosts the performance significantly as we don’t have to manage the dotnet process (earlier approach could have reliability issues as well) and request doesn’t have to travel outside of the process.

ASP.NET Core 2.2 allows out of process deployment as well. When we publish our application, it generates a web.config which has following xml node

<aspNetCore processPath=”dotnet” arguments=”.\InProcApp.dll” stdoutLogEnabled=”false” stdoutLogFile=”.\logs\stdout” hostingModel=”InProcess” />

Here hosting model at the end defined as InProcess. We can change it to OutOfProcess which would be similar as earlier one and application would be running using dotnet.exe. These configuration can also be set via Visual Studio while debugging as

Go to Solution Explorer -> Right Click on project-> Debug (tab)-> Web Server settings section

Performance comparison

As mentioned above, with ASP.NET Core 2.2 allows us to host both the In-process and Out-of-process model (It is similar to earlier version). I have done sample load test using the Netling (know more about this tool here) and for out-of-process result is here

We can see that 2576 request got served per second. I changed the hosting as In-process and ran the same test again and the results are

Here we can see that request per second got increased significantly to 3742 which is approximate ~50% increase. Other data points like median, stddev also got reduced significantly. Itmay vary based on the scenario as I ran it on a developer VM and the application used was a default sample application using core 2.2 (not an empty application). However, Microsoft ran the test in performance labs where they got 4x throughput with In-process hosting.


Even kestrel was introduced with ASP.NET Core as a highly performant web server or as an alternate to IIS, it was always suggested to use IIS as frontend server in windows environment. Initially, many important features were missing in Kestrel which got added with the release of core 2.0 and 2.1, still IIS is advised to use for enterprise environment and internet facing application mainly due to security and stability reasons. There were several bottlenecks with having two different processes (w3wp.exe and dotnet.exe) and the way like port conflicts/not available, process management etc. All these can be avoided using In-process hosting model.


Hosting ASP.NET Core Applications on IIS – A Detailed Look

In last few years, I spent a significant amount of time researching, writing, speaking on ASP.NET application’s performance. Performance of a web app is not just its code but it depends a lot on the hosting platform, configurations and the usage of available resources etc. If you are hosting ASP.NET Webforms/MVC application on IIS, then you can follow below tips and get benefitted quickly.

12 tips to increase the performance of your ASP.NET application drastically – Part 1
12 tips to increase the performance of your ASP.NET application drastically – Part 2

Whenever I think about the performance of any web application, there are three major areas comes into my mind (I am not considering the external factors)

  1. Hosting Server
  2. Application itself
  3. Database/Third Party APIs etc

Any application can’t be highly performant until all three are properly optimized. It can perform better if all were taken in consideration in early stages development lifecycle.

ASP.NET Web Forms is/was very popular web framework but it has many known performance issues. To overcome many and to use latest standards and best practices, Microsoft introduced ASP.NET MVC framework. Over time, Microsoft sensed the need of new highly performant cross platform web framework to compete in market and introduced the all new ASP.NET Core framework (Initial Name: ASP.NET 5) which is written from scratch. Although at a high level, most of the constructs are similar with ASP.NET MVC but the underlying engine got completely rewritten.

While working on ASP.NET Core, MS worked on the complete stack, like for backend – they introduced Entity Framework core and for hosting the application, a new highly optimized web server, known as Kestrel. But as we know IIS provides an array of features, configuration and battle tested for all kind of scenarios, Kestrel was not ready as an edge server.

Although the new server was introduced with ASP.NET Core but it appears that initially, the main focus was on ASP.NET core and other backend frameworks. During ASP.NET 1.X, it was advised to used IIS as frontend server which in turn forwards the requests to Kestrel. A reverse proxy was suggested due to security and reliability reasons. It didn’t have defense against attacks and other configurations like various timeouts, size limits, concurrent connection limits etc. So, we only had the option to deploy the application using IIS (Nginx, Apache for other platforms) as reverse proxy. In the newer versions (ASP.NET Core 2.X), lot of enhancements has been made in Kestrel and with ASP.NET Core 2.1 and later, Kestrel started supporting https (which is nowadays basic requirement for hosting any web application on internet). Now it can be used as internet facing server . In this post and coming post, we will discuss the available deployment options with IIS and explore that what is happening behind the scene.

Using IIS as a Reverse Proxy:

In this scenario, ASP.NET Core application is hosted on kestrel which sits behind IIS. At a high level, it looks like




I created an core sample application which I deployed on IIS. Let’s take a quick look into the steps for deployment.

  1. First, we need to configure IIS on Windows (if it’s not there).
  2. Install the .NET Core Hosting Module (Can be downloaded from here based on the versions).  After installation, you can go to the IIS modules section and ANCM will appear as
  3. Create the website at IIS
    1. Create a folder which will contain app’s published folders, files and binaries
    2. Create a new logs folder inside the earlier created which will contain the logs created by ASP.NET Core module when stdout is enabled.
    3. In IIS, Add a new Website by right clicking on Sites folder under Connections -> ServerName as NetCoreProxy (say).
    4. It by default creates a new application pool named as preferably -. Go to application pools under Connections-> Server Name and Click on NetCoreProxy. Select “No Managed Code” under .NET CLR version 
      “No Managed Code” why? We will discuss it later.
    5. Check the identity which is by default as “AppPoolIdentity” and change it if needed.
  4. Last step, publish the website by Right Clicking on the project in Visual Studio in the newly created folder at 3 a.

Publishing the ASP.NET Core App

While publishing an ASP.NET Core app, we need to select the deployment mode option which has two options: Framework dependent and Self-Contained. Publishing with second option produces a significantly larger binaries because it contains the coreclr and other required system libraries. This should not be a preferred option until necessary because of the huge size and performance implications as it loads all the resources and JIT them on the fly.

Now we should be able to browse our ASP.NET core web site.

Let’s take a look in details

The first question that arises is why do we need to install something on IIS? One of the problems with earlier versions of ASP.NET, that ASP.NET and IIS both has its own pipeline which contains multiple modules (IIS pipeline contains native and managed modules) and each request has to go through both the pipelines invoking each module (However this problem was resolve till certain extent using Integrated Pipeline setting at IIS). Most of the times, many of them are not used by the appliccation. Using ASP.NET Core, we got a new pipeline which has list of middleware that are added at application startup based on the need. To avoid all the IIS overheads, when we install the bundle on IIS, it adds a native module AspNetCoreModule (ANCM) which is invoked at very early stage and forwards the requests to Kestrel as soon as it reaches to IIS. Earlier it was done by an existing module called HTTP Platform Handler which used to forward the request but MS decided to create a new native module ANCM which was fork of HTTP Platform Handler to have better control and able to provide tailored features.

Also, we have seen in deployment steps that we selected “No Managed Code” while configuring the application pool, it means we don’t want to run any managed modules for the requests of this application. As mentioned earlier, IIS pipeline contains the native and managed modules both, ASP.NET core requests do go through some of the native modules like authentication modules (Anonymous, basic, Windows etc), dynamic compression etc. Few native modules are not part of the process and as many of them has a corresponding core middleware (for details, look here). Let’s take a look that how does it work

We can see here that ASP.NET core app runs into a different process dotnet.exe, not in the worker process which was the case with earlier versions of ASP.NET. I also used the process explorer to see it

Here we can see that dotnet.exe is a different process which runs the application. What is this Console Host process? If we take a look in ASP.NET core’s startup class, we will see a main method, similar to console application which is the first method which gets called when the application starts.

Let’s go through step by step process, how a request is served in this scenario

  1. The request is received by the HTTP.sys from the network.
  2. If response is cached at HTTP.sys then it is sent back from there else gets a place the corresponding Application Pool’s queue.
  3. When a thread is available in the thread pool, it picks up the request and start processing it.
  4. The request goes through IIS processing pipeline. As mentioned earlier the request goes through few native IIS modules and once it reaches to ANCM, it forwards the request to Kestrel (under dotnet.exe).
  5. ANCM has a responsibility to manage the process as well. If (re)starts the process (if not running or crashed) and IIS integration middleware configure the server to listen the request on port defined in environment variable. It only accepts the requests which originates from ANCM.
    Note -Please do note that in ASP.NET Webforms/MVC the application is hosted under the worker process w3wp.exe which is managed by Windows Activation Service (WAS) which was part of IIS.
  6. Once the request is received by Kestrel, it creates the HTTPContext object and request is handed over to ASP.NET Core middleware pipeline.
  7. The request is passed to routing middleware which invokes the right controller and action method (model binding, various filters almost similar way as earlier versions).
  8. Finally, the response is returned from the action and passed to kestrel via Middlewares and later sent back to client via IIS.

So we can see the request processing is quite different than earlier versions ASP.NET apps hosted on IIS.

Using Kestrel as an Edge Server

With ASP.NET Core 2.1, Kestrel got lots of new capability which makes it capable of using it an edge server, however for the enterprise level application, it is still recommended to use it behind a proxy. Obviously, one of the first choices is using IIS in windows environment. With ASP.NET Core 2.2, It got some more refinement which makes IIS a better option. We will discuss that in next blog post. Kestrel as an edge server looks like

As this post is specific to IIS hosting, I will not go into the detail but earlier lots of important web server features like Keep alive timeouts, connection timeout, body reading timeouts, request timeouts, size limits, concurrent connection limits, memory limits etc were not available but now most of the configuration can be done and it supports https as well. Even with that Microsoft suggests using IIS as a reverse proxy due to additional feature, security, configurations and many more.

Hope you have enjoyed the post. Do share the feedback. In next post, We will discuss the In-process enhancement  in ASP.NET Core 2.2 in detail.


Wish you all a very Happy New Year – 2019

As 2018 comes to an end and we have just entered to 2019, I would like to wish you and your families a very Happy New Year. New year brings new ideas, new challenges and lot more opportunities. May you rediscover new strengths, garner faith in you, to face the challenges and reach to new heights in your career.

Heartiest thanks to all my elders, readers, subscribers and followers for the kind support and continuous feedback. I will strive to spend more time and add value to community.

Wish you all again a very Happy New Year!


Received Microsoft Most Valuable Professional award 8th time in a Row

Hello friends,

I am very happy to share with you all that I have been awarded as Microsoft Most Valuable Professional for 2018-2019 in Visual Studio and Devlopment Technologies category. This has been 8th award in a row and this wonderfull journey started on 1st July 2011.  Since then 1st July have been a very special day for me as this is the announcement date and day for refreshing the inbox n number of times. I sincerely thank Microsoft for the award and Continuous recognition.

Also, I want to take this opportunity to thank all my readers, followers, seniors and friends throughout these years. It has never been possible without your candid feedback throughout these years.

Initially, I got this award way back in July 2011 in ASP.NET/IIS category which got changed to Visual Studio Development Technologies in 2016. This has been a long and very exciting journey and I have enjoyed each year as part of community and connecting all of you in various ways.

If you also want to know more details about this award you can refer MVP Award Home page and want to get this prestigious award then get the details from here. You can also contact me for any specific details on my email id brij.mishra [at] outlook [dot] com or tweet me at @code_wala and I will be very happy to guide you.

At last, I would say that I’ll devote more time to community with the same intensity and share my knowledge in different ways and learn from all of you. Your feedback is utmost important for me that I’ll seek every time.

Thanks again to all of you for your feedback and support.


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.


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.