跳轉到

Features

Types

using System.Collections;

public class Program
{
    public static void Main(string[] args)
    {
        var myList = new List<string>();
        var myDictionary = new Dictionary<string,int>();
        var myQueue = new Queue<string>();
    }
}

Operations for Enumerable

public class Program
{
    public static void Main(string[] args)
    {
        var myArr = new int[] { 1, 2, 3, };
        var res = myArr.Where(elem => elem > 1).Select(elem => elem * 10).Aggregate(0, (acc, cur) => acc + cur * 2);
        Console.WriteLine(res); //100
    }
}

Language-Integrated Query (LINQ) - Query Syntax

public class Program
{
    public static void Main(string[] args)
    {
        int[] nums = [8, 6, 4];
        IEnumerable<int> filteredNums = from num in nums
                                        where num > 5
                                        select num;
    }
}

Generator

IEnumerable<int> square(IEnumerable<int> data)
{
    foreach (int elem in data)
    {
        var res = Math.Pow(elem, 2);
        yield return (int)res;
    }
}

Delegate

namespace dotnetDelegateTest
{

    public delegate void WriteSometing(int number);

    class Program
    {
        public static void PrintNumber(int number)
        {
            Console.WriteLine($"PrintNumber:{number}");
        }

        public static void SquareFunction(int number)
        {
            Console.WriteLine($"SquareFunction:{Math.Pow(number, 2)}");
        }

        public static void RadicalFunction(int number)
        {
            Console.WriteLine($"RadicalFunction:{Math.Sqrt(number)}");
        }

        static void Main(string[] args)
        {
            WriteSometing delegateTestB = new WriteSometing(SquareFunction);

            delegateTestB += RadicalFunction;

            delegateTestB += PrintNumber;

            delegateTestB += SquareFunction;

            delegateTestB.Invoke(25);
        }

    }
}

Generics

string GenericFunction<T>() where T : IThing
{
    return typeof(T).GetType().Name;
}

Asynchronous Programming

async Task SomeAsyncFunction()
{
    await Task.Delay(2000);
    Console.WriteLine("OK");
}