Login Register





Some new C# concepts filter_list
Author
Message
Some new C# concepts #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.

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:
[Image: HCVu6hR.png]

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
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.

[+] 1 user Likes phyrrus9's post
Reply

RE: Some new C# concepts #2
bro i am beginner and want to learn c# kindly tell me some time so i can start learning it...

Reply

RE: Some new C# concepts #3
well wrote thread tbh i dont even know c# still read it tho lmao
Human Manipulator
[Image: nicelane.gif]

Reply

RE: Some new C# concepts #4
(10-22-2020, 07:06 AM)fsociety Wrote: bro i am beginner and want to learn c# kindly tell me some time so i can start learning it...

Pick a project and struggle through it, that's the only way to learn.

[+] 1 user Likes phyrrus9's post
Reply

RE: Some new C# concepts #5
(10-22-2020, 09:45 AM)phyrrus9 Wrote:
(10-22-2020, 07:06 AM)fsociety Wrote: bro i am beginner and want to learn c# kindly tell me some time so i can start learning it...

Pick a project and struggle through it, that's the only way to learn.

yes this is true like reverse engineering ... just get into it and learn if we really want...

Reply

RE: Some new C# concepts #6
(10-15-2020, 09:29 AM)phyrrus9 Wrote: 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.

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:
[Image: HCVu6hR.png]

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
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.
ngl i have seen some manual obfuscation technique doing this just to mess people up (they normally also add a bunch of gotos), its really easy to go thru it lol

i don't really see the use of it but thats a thing i guess lol
*schlattwalks to .NET app*

[Image: watersheep.gif]
misonothx.rocks
github account

Support @Crimin4L via his website CTRLHub.co & Anon.City !

sinister.ly <3

Reply







Users browsing this thread: 1 Guest(s)