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.


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.


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


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


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}");


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}");


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.


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.


Use the awesomeness of Pattern Matching with C# 7.0

Pattern Matching is a new feature which was introduced with C# 7.0 which allows us to write cleaner and concise code in many different scenarios. This feature can be said as an extension of is and as operators that we already have in C#. I wrote a post on it earlier, you can take a look here. This feature can be broadly devivded in two sections

  1. Using Is Expression
  2. Using Pattern Matching in Switch Statements

Using Is Expression:

Prior to C# 7.0, Is operator was used to check the type of a variable and based on the type, it returns true or false but with C# 7.0, Is Expression provides following three types of pattern matching

  1. Const Pattern
  2. Type Pattern
  3. Var Pattern

Let’s discuss each with examples

Const Pattern: It allows us to check an object with any value. Let’s see an example

static void IsConstExpression()
    Object obj = 2;
    string name = "Brij";

    // null check
    if (obj is null)
        Console.WriteLine("Obj is null");

    // Constant value check
    if(obj is 2)
        Console.WriteLine("Obj has value 2");

    // String value check
    if(name is "Brij")
        Console.WriteLine("name has value \"Brij\"");

Above, we can see that we can check to any value including null.

Type Pattern: It allows us to confirm the type of the object and also assigns the value to a new variable of the given type. Prior to C# 7.0, we also had the similar feature but here we can assign to the new variable as mentioned. Lets see the example

static void IsTypeExpression()
    Object obj = 2;

    var objPerson = new Person() { FirstName = "Brij", LastName="Mishra" };
    var objNewPerson = new Employee() { FirstName = "Anvit", LastName = "Mishra", Company ="ABC Ltd" };

    if (obj is int i)
        Console.WriteLine($" Variable i has the value {i}");

    if (objPerson is Person person)
        Console.WriteLine($" p is of type {person.GetType().Name} and first name is {person.FirstName}");

    if (objNewPerson is Employee newPerson)
        Console.WriteLine($" p is of type {newPerson.GetType().Name} and first name is {newPerson.Company}");

    if (objNewPerson is Person objPer)
        Console.WriteLine($" p is of type {objPer.GetType().Name} and first name is {objPer.FirstName}");


Lets see the output


Here Employee inherits from Person. So in the last statement, we have the actual object of Employee but it assigned to a variable of base type.

Var Pattern: This is special case pattern where we check the type as var. This has one difference with the type pattern as it returns true even if it is null. Let’s see example

static void IsVarExpression()
    Object obj = new Person() { FirstName = "Brij", LastName = "Mishra" };

    if (obj is var p)
        Console.WriteLine($"Var Pattern : P is of type {p?.GetType().Name}.");

    obj = null;

    if (obj is var per)
        Console.WriteLine($"Var Pattern : P is of type {per?.GetType().Name}.");

The output will be as

Here we can see that the second check is also true but since it is null, nothing it displayed where we wrote the type name. Now lets move to switch statements.

Pattern Matching in Switch Statements:

The patterns that we discussed in previous section, can be leveraged in switch statements as well. This becomes very handy when the number of testing conditions grows and using Is pattern becomes tough to maintain.

Earlier switch statement was only supporting constant pattern with limited value types and strings but now we can use Type and var patterns as well. Let’s see the example

    case Manager objM:
        Console.WriteLine("p is of Manager type");
    case Employee objE:
        Console.WriteLine("p is of Employee type");
    case Person objP:
        Console.WriteLine("p is of Person type");
    case null:
        Console.WriteLine("p is null");

Here Person is the base class and with the hierarchy Person->Employee->Manager. Based on the type of p case statement gets executed and in case of null, last one gets executed. Here, the key is the most derived type should be first statement. Compiler also gives a warning if the order is not correct. C# 6.0 also provides us capability to use When clause in switch case. Let’s see the example

    case Employee objE when objE.Grade == 5:
        Console.WriteLine($"{objE.FirstName} is Manager");
    case Employee objE when objE.Grade == 5 && objE.Rating >= 3:
        Console.WriteLine($"{objE.FirstName} is Manager and eligible for additional bonus");
    case var objVar:
        Console.WriteLine("P is not a Employee and is of Type {objVar?.GetType().Name}   ");

Here we can see we can add additional filters in case statements which can be very useful in many scenarios. It also allows to add more that one filter and have various kind of combinations. I have used the var pattern as well in last case statement.

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


How to use Null Conditional operators

Null values are very special in any language, specifically in C#. Every .NET developer must have pulled his/her hair at some point or other while dealing with it. C# is getting mature by adding more capabilities to handle the null values so that we don’t need to write long repetitive code to handle it. This operator was introduced with C# 6.0 sometimes also referred as safe navigation operator, and can be panacea for the most common NullReferenceException. There are few related topics like Null Coalescing operator, Nullable types, which I will advise to take a quick look before proceeding.

What is Null Conditional Operator?

This is a new operator introduced with C# 6.0, helps in performing null check while accessing its members. It provides following two syntax

  1. First operator: ?.   (Also called elvis operator)
  2. Second operator: ?[

Let’s see it with examples

First Null Conditional Operator (?.):

This operator allows us to access the members or elements of the provided instance only when it is not null. So before moving further, lets create few classes for the example

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

    public string LastName { get; set; }

    public int Age { get; set; }

    public List <Address> Addresses { get; set;}

    public Person Spouse {get; set;}

public class Address
    public string Address1 { get; set; }
    public string Address2 { get; set; }

    public string City { get; set; }

    public string State { get; set; }

    public string Zip { get; set; }

If you have to access properties of the person, the usual way of doing it

if(p != null)
    string name = p.FirstName;
    int age = p.Age;

Now lets see using the ?. operator

string name = p?.FirstName;
int? age = p?.Age;

Here we dont need to have a null check as it is taken care by the null conditional operator. name is assigned to null if either object p is null or FirstName is null. While for age, I had to change the age variable as Nullable type because for value types, this operator returns a nullable type which is easily understandable as it would have no value (or null) if p is null.

Now if want to access the properties of Spouse then normally code would be as

if(p != null && p.Spouse != null)
    string name = p.Spouse.FirstName;
    int age = p.Spouse.Age;

which can be written as

string name = p?.Spouse?.FirstName;
int? Age = p?.Spouse?.Age;

Here, we avoided two null check and code looks much cleaner and concise. This operators becomes more helpful for the descending types. You must have written similar code like below numerous times while handling a service response

if (response != null && response.Results != null && response.Results.Status == Status.Success)
    // Handle response

this can be as

if (response?.Results?.Status == Status.Success)
        // Handle response

It looks super clean now. It could be more granular based on the scenario. Now let’s see another operator.

Second Null Conditional Operator (?[): This operator allows you to have a check over index operator. Let’s see the example.
Here I want to see the city of the second address of the person so normally, we will write it as

if(p!=null && p.Addresses != null && p.Addresses.Count > 1)
    string city = p.Addresses[1].City;

This we can write now as

string city = p?.Addresses?[1]?.City;

Now you can assume if I have read the same for spouse then how consize code would be.

Other Usages:
It can be used while calling method of instance like


We can use it with delegate and events as well like


// Note : myCustomDelegate?(args) won't work and it will be an compile time error

myCustomEvent?.Invoke(this, args);

I found this feature very useful in our day to day coding as we save lot of repetitive code. Hope you find it useful as well.


String Interpolation in C# with Examples

String is one of the most used types in C# and it provides many options playing with it. I wrote a post earlier where I discussed about String, StringBuilder and String.Fromat. You might want to have a look here. Today I am going to one of the very nice features that got introduced with C# 6, named String Interpolation.

What is String Interpolation?

String Interpolation is a process of generating the output string from a string literal by replacing the placeholders with the actual values. These placeholders could be just variable names or some expressions. It is like template processing where the variables are replaced with actual values to generate the final output. We already have String.Fromat where the values are replaced based on the indexing of provided parameters but we don’t need that now. Interpolation is very common feature in many new libraries and frameworks like Angular.

We will see multiple examples based on various usage of string interpolation. Lets start with basic one

Example 1:

string name = "Brij";
string topic = "String Interpolation";

// Prior to C# 6
Console.WriteLine(string.Format("Hello {0}. Lets learn {1}",name, topic));

// C# 6 and later
Console.WriteLine($"Hello {name}. Lets learn {topic}");

Here both will give the same output. The second option is much cleaner and readable.  As in the above example, to use it, the string should start with $. Let’s see the syntax of interpolated string

$"<text> {<expression> [,<field-width>] [:<format-string>] } <text> ..."  
  • field-width – It defines the width of the field. If positive then value is right aligned and if negative, it is left aligned. If the value is less than the width of the value, there is no impact in the output.
  • format-string – It can be used to define the format of the value like datetime, currency, number etc.

Note – There should be no space between $ and ” at the start of the string else it will htow an compile time error.
Example 2:

int langversion = 6;
string VSVersion = "Visual Studio 2015";
DateTime releaseDate = new DateTime(2015, 7, 20);

Console.WriteLine($"C# {langversion: 0.0} was released with {VSVersion} on {releaseDate: MMM d yyyy}.");

// output

C#  6.0 got released with Visual Studio 2015 on  Jul 20 2015.

If we have any special characters that could provide different meaning to literal then it should be escaped like “, \ etc . If we want a curly braces ({ or }) in the string then it should be written twice as {{ or }}. To use few language elements like colon (:) in expression, it should be delimited by parenthesis as well in expression.

Example 3:

Console.WriteLine($"We can use special meaning characters like \", \\ by escaping it in literal text.");
Console.WriteLine($"We can use {{curly braces}} by using it twice.");
Console.WriteLine($"To use it in expression it should be delimited by paranthesis"
+$" like  2*2 = 4 is {(2*2 == 4? "Correct" : "Incorrect")}");

// Output
We can use special meaning characters like ", \ by escaping it in literal text.
We can use {curly braces} by using it twice.
To use it in expression it should be delimited by paranthesis like  2*2 = 4 is Correct

In the above example, we have seen that how to use special characters in string literals and in expression. Let’s see one more example

Example 4:

Console.WriteLine($"3 power 4 using Math function { Math.Pow(3, 4)} and using custom power "
$"function { CustomPow(3, 4)}.");

// Output
3 power 4 using Math function 81 and using custom power function 81.

Here we can see that we can use any framework class or even custom function in this expression.

Hope you have enjoyed this feature and will start using it.