Program.cs 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// 自动实现的属性
public class Example
{
    private string name;

    public string Name { get => name; set => name = value; }

    public string Id { get; set; }

    public int Count { get; private set; }

    public string UserId { get;}

    public Example(string userId)
    {
	this.UserId = userId;
    }
}


// 匿名类型
var user = new { name = "Gao", id = 996, height = 172.5 };	// 此时编译器会提示匿名类型, 是 new {string name, int id, double height}
Console.WriteLine(string.Format("name {0}, id {1}, height {2}", user.name, user.id, user.height));

var result = from example in examples select new { example.Name };

foreach (var item in result)
{
	Console.WriteLine(item.Name);
}

// 查询表达式
from example in examples
where example.Count > 10
orderby example.Count descending
select $"{example.Name}\t{example.Count}";

// Lambda 表达式
39 40 41
Func<int, int> square = x => x * x;
Console.WriteLine(square(5));

42
// 表达式树
43 44 45 46 47
Expression<Func<int>> add = () => 1 + 2;
var func = add.Compile(); // 创建一个 delegate
var answer = func(); // 调用 delegate, 得到值3
Console.WriteLine(answer);

48
// 扩展方法
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
public static class Extensions
{
    public static bool IsContainQ(this String str)
    {
        foreach (var item in str)
        {
            if (item.Equals('Q') || item.Equals('q'))
            {
                return true;
            }
        }

        return false;
    }
}

65
// 隐式类型本地变量
66 67
var i = Hello CSDN!”;

68
// 分部方法
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
partial class PartialClassTest
{
    readonly string str = "Hello CSDN!";

    partial void PartialMethodTest();

    public void Display()
    {
        PartialMethodTest();
    }
}

partial class PartialClassTest
{
    partial void PartialMethodTest()
    {
        Console.WriteLine(str);
    }
}


// 对象和集合初始值设定项
public class Cat
{
    // 自动实现的属性
    public int Age { get; set; }
    public string Name { get; set; }

    public Cat()
    {
    }

    public Cat(string name)
    {
        this.Name = name;
    }
}

Cat cat = new Cat { Age = 10, Name = "Fluffy" };
Cat sameCat = new Cat("Fluffy"){ Age = 10 };

var testA = new { name = "Gao", age = 996 };

List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 

public class MatrixTest
{
    private readonly double[,] storage = new double[3, 3];

    public double this[int row, int column]
    {
        get { return storage[row, column]; }
        set { storage[row, column] = value; }
    }
}

var identity = new MatrixTest()
{
    [0, 0] = 1.0,
    [0, 1] = 0.0,
    [0, 2] = 0.0,

    [1, 0] = 0.0,
    [1, 1] = 1.0,
    [1, 2] = 0.0,

    [2, 0] = 0.0,
    [2, 1] = 0.0,
    [2, 2] = 1.0
};