What is New in C-Sharp 9 ( Developing Notes )


1. Instead of writing public class person, you now write public record person
    -
This gives you an ability to instantiate a Record person by utilizing a "With" directive. 
        e.g. 

var OtherPerson = person with { propertyInApersonRecord = "NameOfperson" }
//Constructing a Record Class
record Car(string color, string model)
{
//If you don't utilize the properties passed in as paremeter then you can still
//access them as init properties when instantiating an object using a "with" directive.
public string SomeOtherProperty {get; init; }
}

//You would use the above record like so:
record Car("blue","X3");
//You can also add other Constructors if you like:
record Car("blue","X3"){
public Car(): this(null,null){}//This is an empty Constructor
}

- It copies over all the properties in a person (record) to a new object
- This allows you to have before and after the state of a property in a record, and then you can do diffing, finding the difference. 
[Can't Do]: Currently you cannot inherit between records and classes
[Can't Do]: The shorthand for declaring a record can not be used when declaring a class. It only exists for creating records.
[Note] You can use the record type in C# 9 if you want to utilize Value Semantics in your code.

[Note] Records or a C# file with a "record" is a class but with other features than a C# class.

2. You can now make properties in a class immutable by utilizing an init just like you would use setters and getters.

record Car //Just like class Car, now you can do: record Car
{
public string Color {get; init; } 
//This will make the property 
//immutable but will allow you 
//to assign value when you instatiete
// a new object using a "with" directive
}

- Look at init as the new version of set

3. If you only have one C# file, there is no need to type the main method (which is the entry point to the application). However, if you happen to have two C# files in the program then you will need to define an Entry Point (Main Method)
- This does not mean that you can't use good all "args", you can still pass parameters from Command Line into an Application by simply typing "args[0]" or "args[1]" in your C# code.

4. Pattern matching
- You can now switch on Objects like so:

//Type Patterns and Logical Patterns
//and, not null, when

Car c => c.price switch
{
> 40 => 10 + 2.00m,
< 20 => 1.00M,
_ => 100, //Otherwise, default.
}

//You could even do
Car c => c.price switch
{
> 40 => 10 + 2.00m,
< 20 => 1.00M,
>= 30 and <= 34 => 100, //Otherwise, default.
}

//You could also use "When"
Car(var price,var model) when ((int)price%2) == 0 => 40 //When that condition is met then the price is 40.

- You can even have a light bulb (Intellisense) appear letting you know if you want to convert an If Statement to Switch Statement.

 


#C
# 9
C-Sharp
published
v.0.01



Josh said:

Great, thank you. This helped much.

Posted On: January 24, 2024 13:39:07 PM


For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email Address[email protected]