C# is one of the high-level programming languages, it is used in many business applications, Games, and IoT Devices. Underneath the Architecture lays a powerful platform called Asp.Net or .Net that compiles and runs c#. There is a huge community around C# and more documentation is being written on the Microsoft Website.
In this article, we will look at what is new in C# 9:
- You can now create Records in C# 9 and the latest Build of Visual Studio 2019 along Asp.Net 5.0.
- Records are powerful than classes, look at it as an extension of Classes in C# 8
- Records can allow shallow copies of instance and still respect immutability
( more like object with value symmetric, meaning you can't change the existing of a Record value but you can create a shallow copy of that instance and then change the property on that instance )
- If you decide to use Records in your C# Application, you can not inherit from Classes, only Records can inherit from Records.
For Example, you can not create a class and want to inherit from Records and vise versa.
- With Records, it is now possible to print the values of the properties by simply typing:
The values of that Record will now be printed to the Console or Terminal Window.
- Use the "With" keyword to create a shallow copy of a Record Instance, using "With" all the values in the Parent Record will be copied to a new Object.
- Use the "Init" keyword when declaring Getters and Setters in order to have both property immutability as well as have an ability to create Shallow Copies and change values.
- Records allows Developers to do pretty much everything they are used to do in Classes except that they are an enhancement of Classes.
- Creating a Constructor with Parameters can automatically map to properties declared inside the Record.
- You can use DeConstructors to break up the properties inside the Record so that you can use them separately.
var (MyLovelyPropertyName,AnotherLovelyPropertyName) = YourInstanceOfRecordYouCreatedUpTop;
int anotherValue = MyLovelyPropertyName;
Why Would A Developer Want to Use Records Instead of Classes?
- Records are Value Centric Types, meaning when you had Classes and try to reference or create a new object, you have actually pointed by Reference. Records have Value Summentic, meaning when you point to a new Record you point by Value.
[NB] Let me know what you think about the "Record" in C# 9, will you be using it in any of your projects?
Value Type Vs Reference Type in Memory Allocation C# Application
- C# Types can be stored as Value Type or Reference Types, Integer, Boolean, Doubles are value type meaning, they are stored in the memory at the same location where the value is stored. While Reference Types like Strings and Arrays are stored by Reference meaning where the Values are stored resides a pointer to where the Type itself is stored.
- Looking at Memory, there are two types/kinds of Memory, a Heap, and a Stack. A Heap stores object that is short-lived like valuables that are declared inside your function, those get destroyed when the operation ends inside your function. A Stack stores objects that are long-lived like Static Variables, Singletons, etc.
Value Types are stored on the Stack Memory, however, if you cast an Integer into an Object that becomes a Reference Type and will be stored on the Heap Memory. If you happen to be in this scenario, the type will take up a lot of memory because it might be stored on both Heap and Stuck, and when retrieving it will be looked for in both places.