Fragrance
|
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
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. |
Categories
All
Archives
May 2020
|