Code Mahek
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot

Fragrance
​of Programming

C# 9.0: Record Type

12/9/2020

Comments

 
I recently updated my community version of Visual Studio 2019 which includes the latest .NET 5 and C# 9.0. Microsoft have released lots of new features in the latest .NET version and you can take a look at here for more details.
Meantime I will focus more on one particular feature which is quite interesting to me i.e. C# Records

Record types are a reference type that provides synthesized methods to provide value semantics for equality.

So basically we can create multiple instances of record and compare them for equality. Below is the example

    
It’s very easy, we simply declare our type as a record instead of a class and rest everything is similar to class. Now lets create some instances.

    
Now you can see from the above code that we are comparing two different instance with the property having the same value.
With "Records" it will return true but instead of "Records" if we have used "Class" then it would have return false since class would compare equality by reference.

Another difference is, if you use "ToString()" then record will return something like
"Employee { FirstName = Mital, LastName = Daftary }"

C# Records looks very promising feature and I hope this article is helpful in explaining the basics.

Comments

C# 8.0 Index and Range Operators

12/1/2020

Comments

 
In the latest version of C# i.e. 8.0 Microsoft added two new operators i.e. Index and Range operators. Also, this features is only available in .Net Core 3. In this blog I will try to explain that in a simpler way.

Index operator (^)
Previously C# had no way of indexing the collection from the end, so with unary operator hat (^)  now developer can index from the end. Below is the example

    
One more thing to consider is that the purpose of unary operator hat (^) in regex expression is different compared to used in indexing. Index operators works for all the operators which has a Count or Length property and single integer indexer.
So it will work fine for both Array and List but won't work for Dictionary or HashSet since its a set collection i.e. unordered.

Range Operator (..)
The purpose of the range operator is to slice the collection. Below is the example

    
In range operator, start index is always inclusive and end index is always exclusive. Also range operator works only for the array and not for List

Conclusion
I personally liked both Index operator and the Range operator but bit restricted since I can't use range operator for List.
Comments
<<Previous

    RSS Feed

    Categories

    All
    Azure
    C#
    Cloud
    CodeProject
    CSharp
    DotNet Core
    Jetbrains
    .NET
    Rider
    Source Generators
    SQL
    Tech News
    Ubuntu
    Visual Studio 2015
    Visual Studio 2019
    Windows
    Windows 10

    Archives

    May 2020
    February 2019
    January 2019
    December 2018
    May 2018
    December 2017
    November 2017
    October 2017
    September 2017
    July 2017
    May 2017
    April 2017
    May 2015
    April 2015
    March 2015

    View my profile on LinkedIn

Location

Contact Us

    Subscribe Today!

Submit
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot