Thursday, April 20, 2023

Top 8 New Features in C# 11

C# 11 is the latest version of the C# programming language, released in November 2021. Here are some of the top features included in C# 11:

1. Global using directives:

You can now add a global using directive to your C# files, which means you don't need to add using statements for commonly used namespaces in every file.
// Instead of adding using statements for every file, you can use a global using directive
// to import commonly used namespaces into every file in your project
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Text;
2. Improved target-typed conditional expressions:

C# 11 allows for more flexible use of target-typed conditional expressions, which can make your code more concise and readable.
// C# 11 allows for more flexible use of target-typed conditional expressions
// Here's an example that uses a target-typed conditional expression to check if a value is null
object? nullableObject = null;
string result = nullableObject is null ? "Object is null" : "Object is not null";
Console.WriteLine(result);
3. File-scoped namespaces:

With file-scoped namespaces, you can define a namespace for an entire file rather than having to include a namespace declaration for each individual class.
// With file-scoped namespaces, you can define a namespace for an entire file
// rather than having to include a namespace declaration for each individual class
namespace MyNamespace;

class MyClass
{
    // Class members go here
}
4. Interpolated string improvements:

Interpolated strings now support string interpolation for expressions that return values of any type, not just strings.
// Interpolated strings now support string interpolation for expressions that return values of any type
int x = 5;
string result = $"The value of x is {x}";
Console.WriteLine(result);
5. Extended support for lambda discard parameters:

You can now use discard parameters in lambda expressions, which can make your code more concise and easier to read.
// You can now use discard parameters in lambda expressions
List numbers = new() { 1, 2, 3, 4, 5 };
numbers.ForEach(_ => Console.WriteLine("Hello"));
6. "and" and "or" patterns:

C# 11 introduces new patterns that allow for more complex pattern matching logic, including the ability to combine patterns using "and" and "or".
// C# 11 introduces new patterns that allow for more complex pattern matching logic
// including the ability to combine patterns using "and" and "or"
object obj = "Hello, world!";
if (obj is string { Length: > 5 } or null)
{
    Console.WriteLine("The object is a string with length greater than 5, or null");
}
7. Improved support for global usings in .NET Standard 2.1 and earlier:

Global using directives are now supported in .NET Standard 2.1 and earlier, making it easier to write cross-platform code.
// Global using directives are now supported in .NET Standard 2.1 and earlier
// Here's an example of using a global using directive to import System.IO
// into every file in your project
global using System.IO;
8. Improved support for nullability:

C# 11 includes several improvements to nullability, including better support for nullable reference types and the ability to specify nullability for parameters.
// C# 11 includes several improvements to nullability
// including better support for nullable reference types and the ability to specify nullability for parameters
void MyMethod(string? nullableString)
{
    // The nullableString parameter may be null, so we need to check for null before using it
    if (nullableString is not null)
    {
        Console.WriteLine(nullableString.ToUpper());
    }
}

How to enumerate an enum in C# ?

In C#, you can enumerate an enum using the Enum.GetValues method. Here's an example:
enum DaysOfWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }

foreach (DaysOfWeek day in Enum.GetValues(typeof(DaysOfWeek)))
{
    Console.WriteLine(day);
}
In this example, the DaysOfWeek enum is defined with seven possible values representing the days of the week. The Enum.GetValues method is called with the typeof(DaysOfWeek) argument to get an array of all the possible values of the enum. The foreach loop then iterates over the array and prints each value to the console. You can also use the Enum.GetName method to get the name of each enum value:
foreach (DaysOfWeek day in Enum.GetValues(typeof(DaysOfWeek)))
{
    string name = Enum.GetName(typeof(DaysOfWeek), day);
    Console.WriteLine($"{day} = {name}");
}
This code outputs the following:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Monday = Monday
Tuesday = Tuesday
Wednesday = Wednesday
Thursday = Thursday
Friday = Friday
Saturday = Saturday
Sunday = Sunday

SOLID Software Design Principles - Brief Explanation

SOLID software principles is the set of principles put forward by Robert C. Martin, which ensures that the software developed is flexible, reusable, sustainable and understandable and prevents code repetition. So the main purpose of these principles are, in order to easily adapted to requirements in the future and easier to add new features without the need to change anything in your code and also minimal change in code despite the new requirements and minimize the loss of time caused by the problems, such as the continuous correction on the code or even re-writing to code.

We have five principles.


1. Single Responsibility Principle

A class should have only one reason to change, i.e., it should have only one responsibility or job to do. This principle helps to keep classes focused, and makes them easier to understand, test, and maintain.

Example:

2. Open Close Principal

Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. In other words, you should be able to add new functionality to a system without changing its existing code. This principle promotes the use of inheritance and polymorphism to achieve this goal.

Example:

3. Liskov Substitution Principle

Subtypes must be substitutable for their base types. This principle states that any object of a derived class should be able to replace an object of its base class without causing any problems or errors. This principle ensures that inheritance is used correctly and that objects are well-behaved in a system.

Example:

4. Interface Segregation Principle

Clients should not be forced to depend on interfaces they don't use. This principle suggests that it's better to have many smaller interfaces than one large interface. This promotes decoupling and helps to prevent changes in one part of the system from affecting other parts.

Example:

5. Dependency Inversion Principle

High-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions. This principle promotes the use of interfaces and dependency injection to decouple modules and improve their flexibility and maintainability.

Example:

These are the main principles in software development and all these principles is very similar to each other.