C# Index and Range

Index

int[] numbers = Enumerable.Range(0, 100).ToArray();
int x = 12;
int y = 25;
int z = 36;

Console.WriteLine($"{numbers[^x]} is the same as {numbers[numbers.Length - x]}");
Console.WriteLine($"{numbers[x..y].Length} is the same as {y - x}");

Console.WriteLine("numbers[x..y] and numbers[y..z] are consecutive and disjoint:");
Span<int> x_y = numbers[x..y];
Span<int> y_z = numbers[y..z];
Console.WriteLine($"\tnumbers[x..y] is {x_y[0]} through {x_y[^1]}, numbers[y..z] is {y_z[0]} through {y_z[^1]}");

Console.WriteLine("numbers[x..^x] removes x elements at each end:");
Span<int> x_x = numbers[x..^x];
Console.WriteLine($"\tnumbers[x..^x] starts with {x_x[0]} and ends with {x_x[^1]}");

Console.WriteLine("numbers[..x] means numbers[0..x] and numbers[x..] means numbers[x..^0]");
Span<int> start_x = numbers[..x];
Span<int> zero_x = numbers[0..x];
Console.WriteLine($"\t{start_x[0]}..{start_x[^1]} is the same as {zero_x[0]}..{zero_x[^1]}");
Span<int> z_end = numbers[z..];
Span<int> z_zero = numbers[z..^0];
Console.WriteLine($"\t{z_end[0]}..{z_end[^1]} is the same as {z_zero[0]}..{z_zero[^1]}");

Range

var words = "my name is mohammed alabdali i am from jeddah i study master".Split(" ");
string[] allWords = words[..]; // all texts
string[] firstPhrase = words[..4]; //my name is mohammed
string[] lastPhrase = words[9..]; //i study master
Range implicitRange = 3..^5;

Range explicitRange = new(
    start: new Index(value: 3, fromEnd: false),
    end: new Index(value: 5, fromEnd: true));

if (implicitRange.Equals(explicitRange))
{
    Console.WriteLine(
        $"The implicit range '{implicitRange}' equals the explicit range '{explicitRange}'");
}
// Sample output:
//     The implicit range '3..^5' equals the explicit range '3..^5'

C# – Multithreading

The Main Thread

using System;
using System.Threading;

namespace MultithreadingApplication {
   class MainThreadProgram {
      static void Main(string[] args) {
         Thread th = Thread.CurrentThread;
         th.Name = "MainThread";
         
         Console.WriteLine("This is {0}", th.Name);
         Console.ReadKey();
      }
   }
}
Sr.No.Property & Description
1CurrentContext Gets the current context in which the thread is executing.
2CurrentCulture Gets or sets the culture for the current thread.
3CurrentPrinciple Gets or sets the thread’s current principal (for role-based security).
4CurrentThread Gets the currently running thread.
5CurrentUICulture Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run-time.
6ExecutionContext Gets an ExecutionContext object that contains information about the various contexts of the current thread.
7IsAlive Gets a value indicating the execution status of the current thread.
8IsBackground Gets or sets a value indicating whether or not a thread is a background thread.
9IsThreadPoolThread Gets a value indicating whether or not a thread belongs to the managed thread pool.
10ManagedThreadId Gets a unique identifier for the current managed thread.
11Name Gets or sets the name of the thread.
12Priority Gets or sets a value indicating the scheduling priority of a thread.
13ThreadState Gets a value containing the states of the current thread.

Creating Threads

using System;
using System.Threading;

namespace MultithreadingApplication {
   class ThreadCreationProgram {
      public static void CallToChildThread() {
         Console.WriteLine("Child thread starts");
      }
      static void Main(string[] args) {
         ThreadStart childref = new ThreadStart(CallToChildThread);
         Console.WriteLine("In Main: Creating the Child thread");
         Thread childThread = new Thread(childref);
         childThread.Start();
         childThread.Join(1000);
         Console.ReadKey();
      }
   

Thread Pool

List<string> urls = new(){
    "https://www.google.com/",
    "https://www.duckduckgo.com/",
    "https://www.yahoo.com/",
};

foreach (var url in urls)
{
    ThreadPool.QueueUserWorkItem((state) => CheckHttpStatus(url));
}

async and await

static void Main()
    {
        Task task = new Task(CallMethod);
        task.Start();
        task.Wait();
        Console.ReadLine();
    }

    static async void CallMethod()
    {
        string filePath = "E:\\sampleFile.txt";
        Task<int> task = ReadFile(filePath);

        Console.WriteLine(" Other Work 1");
        Console.WriteLine(" Other Work 2");
        Console.WriteLine(" Other Work 3");

        int length = await task;
        Console.WriteLine(" Total length: " + length);

        Console.WriteLine(" After work 1");
        Console.WriteLine(" After work 2");
    }

    static async Task<int> ReadFile(string file)
    {
        int length = 0;

        Console.WriteLine(" File reading is stating");
        using (StreamReader reader = new StreamReader(file))
        {
            // Reads all characters from the current position to the end of the stream asynchronously
            // and returns them as one string.
            string s = await reader.ReadToEndAsync();

            length = s.Length;
        }
        Console.WriteLine(" File reading is completed");
        return length;
    }
static async Task Main(string[] args)
   {
     await callMethod();
     Console.ReadKey();
   }

   public static async Task callMethod()
   {
     Method2();
     var count = await Method1();
     Method3(count);
   }

   public static async Task<int> Method1()
   {
     int count = 0;
     await Task.Run(() =>
     {
       for (int i = 0; i < 100; i++)
       {
         Console.WriteLine(" Method 1");
         count += 1;
       }
     });
     return count;
   }

   public static void Method2()
   {
     for (int i = 0; i < 25; i++)
     {
       Console.WriteLine(" Method 2");
     }
   }

   public static void Method3(int count)
   {
     Console.WriteLine("Total count is " + count);
   }

dotnet CLI and Project file

dotnet

run

dotnet run --project ./projects/proj1/proj1.csproj
dotnet run --configuration Release
dotnet run -c Release --arch x64 --project ConsoleTest/ConsoleTest.csproj

#--no-build to run with no build

build

Builds a project and all of its dependencies.

Executable or library output

Whether the project is executable or not is determined by the <OutputType> property in the project file.

<PropertyGroup>
  <OutputType>Exe</OutputType>
</PropertyGroup>

To produce a library, omit the <OutputType> property or change its value to Library. The IL DLL for a library doesn’t contain entry points and can’t be executed.

  • --no-self-containedPublishes the application as a framework dependent application. A compatible .NET runtime must be installed on the target machine to run the application. Available since .NET 6 SDK.
dotnet build --source d1/d2 --output d3/d4 -c Release 

publish

dotnet publish – Publishes the application and its dependencies to a folder for deployment to a hosting system.

dotnet publish [<PROJECT>|<SOLUTION>] [-a|--arch <ARCHITECTURE>]
    [-c|--configuration <CONFIGURATION>]
    [-f|--framework <FRAMEWORK>] [--force] [--interactive]
    [--manifest <PATH_TO_MANIFEST_FILE>] [--no-build] [--no-dependencies]
    [--no-restore] [--nologo] [-o|--output <OUTPUT_DIRECTORY>]
    [--os <OS>] [-r|--runtime <RUNTIME_IDENTIFIER>]
    [--sc|--self-contained [true|false]] [--no-self-contained]
    [-s|--source <SOURCE>] [--use-current-runtime, --ucr [true|false]]
    [-v|--verbosity <LEVEL>] [--version-suffix <VERSION_SUFFIX>]

test

dotnet test

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
    <PackageReference Include="xunit" Version="2.4.2" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
  </ItemGroup>

Project File

item group and property group examples

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        
    </PropertyGroup>
<ItemGroup>
      <PackageReference Include="Google.Cloud.Storage.V1" Version="4.4.0" />
        <None Update="settings.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="cloud_credintial.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="shell">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <Content Include="shell/*.*">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
    </ItemGroup>

C# Topics (Pattern matching)

Pattern matching

switch (aa)
{
    case {val:12}:
        Console.WriteLine("hello");
        break;
    case A aaa when aa.val2==13:
        Console.WriteLine(aaa.val);
        break;
}

Simplifying switch statements with switch expressions


Animal animal = new Animal() { Name = "tiger" };
string message = animal switch
{
    Animal fourLeggedCat when fourLeggedCat.Name=="cat"
        => $"small cat",
    Animal wildCat when wildCat.Name == "tiger"
        => $"big cat",
};
Console.WriteLine($"switch expression: {message}");

class Animal
{
    public string Name { get; set; }
}

with array patterns

int[] mm = new[]{1,2,3,4,5,6,7};
string vv=mm switch
{
    int[] vvv when mm is [_,2,_,4,..] => "one",
    int[] vvv when mm is {Length: 2} => "two"
};
Console.WriteLine(vv);


int[] mm = new[]{1,2,3,4,5,6,7};
string vv=mm switch
{
    [_,2,_,4,..] => "one",
    {Length: 2} => "two"
};
Console.WriteLine(vv);

relational patterns


bool b=v is (> 32) and (< 66);
string WaterState(int tempInFahrenheit) =>
    tempInFahrenheit switch
    {
        (> 32) and (< 212) => "liquid",
        < 32 => "solid",
        > 212 => "gas",
        32 => "solid/liquid transition",
        212 => "liquid / gas transition",
    };

var m=(12,44);
var l=new{val1=12,val2=44};
bool res=m is {Item1:12,Item2:_};
bool res2=l is {val1:12,val2:_};
Console.WriteLine($" m is {m}, and l is{l}");

C# Topics (1)

is operator

Console.WriteLine(new A{val1=1,val2=2} is { val1:1 ,val3:0});//true
public class A
{
    public int val1;
    public int val2;
    public int val3;
}

object o=default(object);
o is null //true
o is not null //false

is with patterns

object greeting = "Hello, World!";
if (greeting is string message)
{
    Console.WriteLine(message.ToLower());  // output: hello, world!
}

int? xNullable = 7;
int y = 23;
object yBoxed = y;
if (xNullable is int a && yBoxed is int b)
{
    Console.WriteLine(a + b);  // output: 30
}

with expresion

with expression produces a copy of its operand with the specified properties and fields modified.
it work only with record and struct for class you can use IClonable or serialization and deserialization
you use object initializer syntax to specify what members to modify and their new values:

public record Point(int X, int Y);
public record NamedPoint(string Name, int X, int Y) : Point(X, Y);

public static void Main()
{
   Point p1 = new NamedPoint("A", 0, 0);
   Point p2 = p1 with { X = 5, Y = 3 };
   Console.WriteLine(p2 is NamedPoint);  // output: True
   Console.WriteLine(p2);  // output: NamedPoint { X = 5, Y = 3, Name = A }
}

stackalloc

allocate stacked memory for performance purposes

const int MaxStackLimit = 1024;
Span<byte> buffer = inputLength <= MaxStackLimit ? stackalloc byte[MaxStackLimit] : new byte[inputLength];
Span<int> first = stackalloc int[3] { 1, 2, 3 };
Span<int> second = stackalloc int[] { 1, 2, 3 };
ReadOnlySpan<int> third = stackalloc[] { 1, 2, 3 };

Touples

public class A
{
    public int val1;
    public int val2;
    public int val3;
    
    (int v1,int v2,int v3) GetVal()
    {
        return (val1, val2);
    }
}


 (int t2,int t3) T = (1, 2);

anonymous object

var m = new { m = 12 };
(int Id, string Name) M=(1, "hello");

List<int> vals = new(){ 1, 2, 3 };

A aa = new(1,2,3);

A aa = new(){val1=1,val2=2,val3=3};

Record

by default record is a class compares the record with == it compares the properties

with == operator
class compare with reference
struct not accept
record compare with values


record A
{
    public int a { get; init; }
    public int b { get; init; }
}

record AA(int a, int b);

extension method

public static class StringExtensions
{
    public static void GobbleGobble(this string s,int val) //new string("").GobbleGobble(12)
    {
        Console.Out.WriteLine("Gobble Gobble, " + s);
    }
}
class Data1
{
    public int Id => 1; //property
    public string Name => "Test";//property
}

record Data2(int Id,string Name); //properties

var ( Id, Name) = new Data2(Id :1, "test");

Deconstruct function in class

class Data
{
    public int Id { set; get; }
    public string Name { set; get; }
    
    public void Deconstruct(out int Id, out string Name)
    {
        Id = this.Id;
        Name = this.Name;
    }
    
};

var ( Id, Name) = new Data(){Id =1, Name="test"};
( int Id, string Name) = new Data(){Id =1, Name="test"};

Reflections and Attributes

The .NET reflection services allow you to discover, using APIs from the System.Reflection namespace, the same information that you can see with the tools mentioned earlier. The key to this process is the type called System.Type, which contains members that expose all of a type’s metadata. This is done with the help of other types from the System.Reflection namespace

class A
{
    public int val;
    public int value
    {
        private set
        {
            val = value;
        }
        get
        {
            return val;
        }
    }
    public A(int i)
    {
        this.val = i;   
    }
}

A a=new A(12);
Type t1 =typeof(A);
Type t2 = a.GetType();
Type t3=Type.GetType("A");
Console.WriteLine((t1 == t2) && (t1 == t3));//true
A aa=(A)Activator.CreateInstance(t1, 12);
t1.GetProperty("value").GetSetMethod(true)?.Invoke(aa, new object[] { 44 });
Console.WriteLine(t1.GetField("val").GetValue(aa));//44

Attributes

Attributes provide meta-information about assemblies, types, and members. This meta-information is consumed by the compiler, the CLR, or tools that use reflection services to read them. Attributes are actually types that derive from the System.Attribute abstract class.

class DescriptionAttribute : Attribute
{
    public string Text { get; private set; }
    public bool Required { get; set; }
    public DescriptionAttribute(string description)
    {
        Text = description;
    }
}
[Description("Main component of the car", Required = true)]
class Engine
{
}
[AttributeUsage(AttributeTargets.Class|
                AttributeTargets.Struct|
                AttributeTargets.Method|
                AttributeTargets.Property|
                AttributeTargets.Field,
                AllowMultiple = true,
                Inherited = true)]
class DescriptionAttribute : Attribute
{
    public string Text { get; private set; }
    public bool Required { get; set; }
    public DescriptionAttribute(string description)
    {
        Text = description;
    }
}
[Serializable]
[Description("Main component of the car")]
[ComVisible(false)]
class Engine
{
}
[Serializable, 
 Description("Main component of the car"), 
 ComVisible(false)]
class Engine
{
}

Attributes In Reflections

var e = new Engine("M270 Turbo", 1600, 75.0);
var type = e.GetType();
var attributes = type.GetCustomAttributes(typeof(DescriptionAttribute), 
                                          true);
if (attributes != null)
{
    foreach (DescriptionAttribute attr in attributes)
    {
        Console.WriteLine(attr.Text);
    }
}
var properties = type.GetProperties();
foreach (var property in properties)
{
    var pattributes = 
      property.GetCustomAttributes(
         typeof(DescriptionAttribute), false);
    if (attributes != null)
    {
        foreach (DescriptionAttribute attr in pattributes)
        {
            Console.WriteLine(
              $"{property.Name} [{attr.Text}]");
        }
    }
}