C#笔记(四)---主要语言区域

数组、集合和 LINQ

泛型集合: System.Collections.Generic
专用集合: System.Span(用于访问堆栈帧上的连续内存),以及 System.Memory(用于访问托管堆上的连续内存)。
所有集合(包括数组、Span 和 Memory)都遵循一种统一的迭代原则。 使用System.Collections.Generic.IEnumerable 接口。

数组

// 多维数组
int[] a1 = new int[10];
int[,] a2 = new int[10, 5];
int[,,] a3 = new int[10, 5, 2];
// 交错数组
int[][] a = new int[3][];
a[0] = new int[10];
a[1] = new int[5];
a[2] = new int[20];

委托和 Lambda 表达式

委托类型表示对具有特定参数列表和返回类型的方法的引用。 通过委托,可以将方法视为可分配给变量并可作为参数传递的实体。委托是面向对象且类型安全的。

delegate double Function(double x);

class Multiplier
{
    double _factor;
    
    public Multiplier(double factor) => _factor = factor;
    
    public double Multiply(double x) => x * _factor;
}

class DelegateExample
{
    static double[] Apply(double[] a, Function f)
    {
        var result = new double[a.Length];
        for (int i = 0; i < a.Length; i++) result[i] = f(a[i]);
        return result;
    }
    
    public static void Main()
    {
        double[] a = { 0.0, 0.5, 1.0 };
        double[] squares = Apply(a, (x) => x * x);
        double[] sines = Apply(a, Math.Sin);
        Multiplier m = new Multiplier(2.0);
        double[] doubles = Apply(a, m.Multiply);
    }
}

Function 委托类型实例可以引用需要使用 double 自变量并返回 double 值的方法。 Apply 方法将给定的 Function 应用于 double[] 的元素,从而返回包含结果的 double[]。 在 Main 方法中,Apply 用于向 double[] 应用三个不同的函数。
委托可以引用静态方法(如上面示例中的 Square 或 Math.Sin)或实例方法(如上面示例中的 m.Multiply)。 引用实例方法的委托还会引用特定对象,通过委托调用实例方法时,该对象会变成调用中的 this。
还可以使用匿名函数创建委托,这些函数是在声明时创建的“内联方法”。 匿名函数可以查看周围方法的局部变量。 以下示例不创建类:

double[] doubles = Apply(a, (double x) => x * 2.0);

async/await

C# 支持含两个关键字的异步程序:async 和 await。 将 async 修饰符添加到方法声明中,以声明这是异步方法。 await 运算符通知编译器异步等待结果完成。 控件返回给调用方,该方法返回一个管理异步工作状态的结构。 结构通常是 System.Threading.Tasks.Task,但可以是任何支持 awaiter 模式的类型。 这些功能使你能够编写这样的代码:以其同步对应项的形式读取,但以异步方式执行。 例如,以下代码会下载 Microsoft Docs 的主页:

public async Task<int> RetrieveDocsHomePage()
{
    var client = new HttpClient();
    byte[] content = await client.GetByteArrayAsync("https://docs.microsoft.com/");

    Console.WriteLine($"{nameof(RetrieveDocsHomePage)}: Finished downloading.");
    return content.Length;
}

Note: return 语句中指定的类型与方法的 Task 声明中的类型参数匹配。 (返回 Task 的方法将使用不带任何参数的 return 语句)。

属性

属性(property)是一种用于访问对象或类的特性的成员。
属性提供灵活的机制来读取、编写或计算私有字段的值。
属性提供了一种机制,它把读取和写入对象的某些特性与一些操作关联起来。
可以像使用公共数据成员一样使用属性,但实际上属性是称为“访问器”的一种特殊方法,这使得数据在被轻松访问的同时,仍能提供方法的安全性和灵活性。

class Program
{
    static void Main(string[] args)
    {
        var d = new DayTemperature();
        d.temperature[0] = 20.5;
        d.temperature[1] = 22;
        //使用类索引器访问
        Console.WriteLine(d[1]);        //22
        Console.ReadKey();
    }
}
class DayTemperature
{
    public double[] temperature = new double[24];
    //类的索引器
    public double this[int index]
    {
        get
        {
            //检查索引范围
            if (index < 0 || index >= temperature.Length)
            {
                return -1;
            }
            else
            {
                return temperature[index];
            }
        }
        set
        {
            if (!(index < 0 || index >= temperature.Length))
            {
                temperature[index] = value;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/francisforeverhappy/p/Csharp4.html