Some new C# concepts 10-15-2020, 09:29 AM
#1
A while ago I made a quick thread about C# Code Density, and in that thread I highlighted pattern matching switches, as well as their one-line use cases.
Well, in this thread, I'm going to show you a sample program, provided mostly without comment about two of the new C# 9 features.
Here we go.
Yes, that is actually a valid C# program, despite it looking almost nothing like one. Here's your proof:
So, this sample program shows you three new features of C# 9:
1. Typeless new()
Ok, so it's not actually typeless, this will only work if you have declared your variable with a static type (no use of dynamic or var).
The operator will simply assume you want to declare a new type of the same type as the variable, and pass any arguments you give it to the constructor.
I'd say this will save some time, as I commonly have types like the following:
Needing to type that out twice is a gigantic pain.
2. Additional patterns in switch and if statements
Prior to C# 9, if we wanted to look at individual components of a struct (or class), we would either need to include them explicitly in our switch (all of them that we wanted to look at).
With this new update, we can simply wrap them in curly braces and access any (public) member within the object.
Additionally, we can do comparisons on a single variable by using words "and", "or", and "not". We need only write the variable once and can include as many conditions as we want.
This is demonstrated outside of the switch statement as well, and provides an easy way to simply if statements for things like between, not between, between x,y OR not between w,z, etc....
3. Classless programs
The astute among you may have noticed that there are no classes in this program, nor are there any methods. With C# 9, you can place code completely outside of a class, provided it appears before any class definitions and is restrained to only one file.
This is really useful for scratch programs people need sometimes to do a simple task with like 30-100 lines of code, but don't want to bother with all the namespace and class setup.
I can also see use cases where people don't want the Main method to be static. Instead of having 2 entry points, you can simply make your runtime class as you want it, then in an entry point file just write the code to init everything.
Well, I hope you learned something new about C# 9 today, because that's all I've got for you.
Please let me know with a reply if you'd like to see more of these kinds of things.
Well, in this thread, I'm going to show you a sample program, provided mostly without comment about two of the new C# 9 features.
Here we go.
Code:
using System;
TestStructure[] tests =
{
new()
{
Age = 25,
Name = "Alice",
Gender = TestStructure.GenderType.Female
},
new()
{
Age = 30,
Name = "Bob",
Gender = TestStructure.GenderType.Male
},
new()
{
Age = 31,
Name = "Bob",
Gender = TestStructure.GenderType.Female
},
new()
{
Age = 19,
Name = "Jack",
Gender = TestStructure.GenderType.Male
},
new()
{
Age = 61,
Name = "Evan",
Gender = TestStructure.GenderType.Male
},
};
Random r = new();
TestStructure test = tests[r.Next(tests.Length)];
bool? IsGoodCryptographer = test switch
{
{ Name: "Alice", Gender: TestStructure.GenderType.Female }
or { Name: "Bob", Gender: TestStructure.GenderType.Male } => true,
{ Age: <75 and >60 } => null,
_ => false
};
string adjective = IsGoodCryptographer switch
{
false => "not ",
null => "possibly ",
_ => string.Empty
};
Console.WriteLine($"{test.Name} ({test.Age}/{test.Gender.ToString()[..1]}) is {adjective}a good cryptographer.");
int z = 2;
bool IsZBetween1And5 = z is > 1 and < 5;
Console.WriteLine($"Is {z} between 1 and 5? {IsZBetween1And5}");
public struct TestStructure
{
public enum GenderType
{
Male,
Female
}
public int Age;
public string Name;
public GenderType Gender;
}
Yes, that is actually a valid C# program, despite it looking almost nothing like one. Here's your proof:
Spoiler:
So, this sample program shows you three new features of C# 9:
1. Typeless new()
Ok, so it's not actually typeless, this will only work if you have declared your variable with a static type (no use of dynamic or var).
The operator will simply assume you want to declare a new type of the same type as the variable, and pass any arguments you give it to the constructor.
I'd say this will save some time, as I commonly have types like the following:
Code:
APIv3.CompanyName.Models.v2.Feed.Flags.EditModel
2. Additional patterns in switch and if statements
Prior to C# 9, if we wanted to look at individual components of a struct (or class), we would either need to include them explicitly in our switch (all of them that we wanted to look at).
With this new update, we can simply wrap them in curly braces and access any (public) member within the object.
Additionally, we can do comparisons on a single variable by using words "and", "or", and "not". We need only write the variable once and can include as many conditions as we want.
This is demonstrated outside of the switch statement as well, and provides an easy way to simply if statements for things like between, not between, between x,y OR not between w,z, etc....
3. Classless programs
The astute among you may have noticed that there are no classes in this program, nor are there any methods. With C# 9, you can place code completely outside of a class, provided it appears before any class definitions and is restrained to only one file.
This is really useful for scratch programs people need sometimes to do a simple task with like 30-100 lines of code, but don't want to bother with all the namespace and class setup.
I can also see use cases where people don't want the Main method to be static. Instead of having 2 entry points, you can simply make your runtime class as you want it, then in an entry point file just write the code to init everything.
Well, I hope you learned something new about C# 9 today, because that's all I've got for you.
Please let me know with a reply if you'd like to see more of these kinds of things.