C#核心笔记——(一)C#和.NET Framework

C#是一种通用的,类型安全的面向对象编程语言。其目标是提高程序员生产力。

一.面向对象

C#实现了丰富的面向对象范式,包括封装、继承、多态。

C#面向对象特性包括:
统一的类型系统
类与接口
属性、方法、事件
C#支持纯函数模式

二、类型安全性

C#是一门类型安全的语言。例如:C#不允许将字符串类型作为整数类型处理。

C#支持类型静态化。能够在程序运行前排除大量错误。

C#允许使用dynamic关键字动态定义指定类型。然而C#在大多情况下仍然是一门静态语言。

C#还是一门强类型语言。

三、内存管理

C#依靠运行时来实现自动内存管理。公共语言运行时的垃圾回收器会作为程序的一部分运行。

C#并未抛弃指针,关键字unsafe的程序块显式内存分配。

四、平台支持

Linux、macOS、iOS、Android

五、CLR

公共语言运行时(Common Language Runtime,CLR),如自动化内存管理和异常处理。

C#也会将代码编译为托管代码。托管代码以中间语言(Intermediate Language,IL)的形式表示。CRL通常会在执行前,将IL转换为机器(如x84、x64)原生代码,称为即时(Just-In-Time)编译。

六、.NET Framework

.NET Framework是由CLR和大量的程序库组成。

七、其他框架

UWP
.Net Core
ASP.NET Core
Xamarin

八、简史

8.1C# 7.0新特性

8.1.1 数字字面的改进

下划线改善可读性:

int million = 1_000_000;

二进制字面量使用Ob前缀来标识:

int a = 0b1001_0000;
var b = 0b1001_0000;

8.1.2 输出变量及参数忽略

调用含有out参数的方法将更加容易:

bool successful = int.TryParse("123", out int result);
Console.WriteLine(result);

当调用含有多个out参数方法时,可以使用下划线字符_忽略你并不关心的参数:

方法

static void SimpleMethod(out int result,out int result2 ,out int result3)
{
    
    
    result = 1 + 1;
    result2 = 2 + 2;
    result3 = 3 + 3;
}

调用

SimpleMethod(out _, out _, out int result2);

8.1.3 模式

is运算符也可以自然的引入变量,称为模式变量

方法

static void SimpleMethod2(object x)
{
    
    
    if(x is string s)
    {
    
    
        Console.WriteLine(s.Length);
    }
}

调用

SimpleMethod2("大家好呀");

同样 switch语句也同样支持模式。可以使用when子句来判断条件,或是直接选择null;

方法

static void SimpleMethod3(object? x)
        {
    
    
            switch(x)
            {
    
    
                case int i:
                    Console.WriteLine("It is an int"); 
                    break;
                case string s:
                    Console.WriteLine(s.Length);
                    break;
                    case bool b when b == true :
                    Console.WriteLine("True");
                    break;
                case null:
                    Console.WriteLine("It is null");
                    break;
            }
        }

调用

SimpleMethod3(12);
SimpleMethod3("12");
SimpleMethod3(true);
SimpleMethod3(null);

8.1.4 局部方法

局部方法是声明在其他函数内部的方法

static void WriteCubes()
{
    
    
    Console.WriteLine(Cubu(3));
    Console.WriteLine(Cubu(4));
    Console.WriteLine(Cubu(5));

    int Cubu(int value) => value * value * value;
}

8.1.5 更多的表达式体成员

C#6引入了以”胖箭头“语法表示的表达式的方法,只读属性、运算符以及索引器。而C#7更将其扩展到了构造函数、读/写属性和终结器中:

类:

 public class Person : IAsyncDisposable
    {
    
    
        string? name;
        public Person(string? name) => Name = name;

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

        public ValueTask DisposeAsync()
        {
    
    
            Console.WriteLine("Dispose");
            return ValueTask.CompletedTask;
        }
    }

方法:

 static async void PersonDispose()
        {
    
    
            await AsyncPersonDispose();
        }

        static async Task AsyncPersonDispose()
        {
    
    
            await using (Person p = new Person("小米"))
            {
    
    
                Console.WriteLine(p.Name);
            }
        }

8.1.6 解构器

C#7引入了结构器模式。构造器一般接受一系列(作为参数)并将其赋值给字段,而结构器则正相反,它将字段反向赋值给变量。

public class Dog
    {
    
    
        string? name;
        public Dog(string? name) => Name = name;

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

        public void Deconstruct(out string firstName,out string lastName)
        {
    
    
            int spacePos = name.IndexOf(' ');
            firstName = name.Substring(0, spacePos);
            lastName = name.Substring(spacePos + 1);
        }
    }

调用

var joe = new Dog("joe Bloggs");
var(first,last)=joe;
Console.WriteLine(first);
Console.WriteLine(last);

8.1.7 元组

C# 7 改进当属显示的元组(tuple)支持,元组提供了一系列相关值的简单方式:

var bob = ("Bob", 23);
Console.WriteLine(bob.Item1);
Console.WriteLine(bob.Item2);

C#的新元组实质上是使用了System.ValueTuple<…>泛型结构的语法糖。
我们还可以对元组的元素命名:

var tuple = (Name: "Bob", Age: 23);
Console.WriteLine(tuple.Name);
Console.WriteLine(tuple.Age);

有了元组函数再也不用通过一系列out参数来返回多个值了:

static (int row, int colum) GetFilePosition() => (3, 10);
var pos = GetFilePosition();
Console.WriteLine(pos.row);
Console.WriteLine(pos.colum);

元组隐式的支持解构模式

(int row,int column) = GetFilePosition();
Console.WriteLine(row);
Console.WriteLine(column);

8.1.7 thow表达式

现在它可以出现在表达式体函数中

public string Foo() => throw new NotImplementedException();

thow表达式也可以出现在三无判断运行符中:
方法

static string Capotalize(string? value) =>
    value == null ? throw new ArgumentException("Value") :
    value == "" ? "" :
    char.ToUpper(value[0]) + value.Substring(1);

调用

Console.WriteLine(Capotalize(null)); 

8.2 C# 6.0新特性

null条件(“Elvis”)运算符,可以避免在调用方法或访问类型的成员之前显式地编写用于null判断的语句。如下result将会为null而不会抛出NullReferenceException:

System.Text.StringBuilder sb = null;
string result = sb?.ToString();

lambda表达式

public int TimesTwo(int x) => x * 2;
public string someProperty => "Property value";

属性初始化器可以对自动属性进行初始赋值:

public DateTime TimeCreated {
    
     get; set; } = DateTime.Now;

这种初始化也支持只读属性:

public DateTime TimeCreated {
    
     get; } = DateTime.Now;

只读属性也可以在构造器中赋值。

索引初始化器可以一次性初始化具有索引器的任意类型:

var dict = new Dictionary<int, string>()
{
    
    
    [3] = "three",
    [20] = "ten"
};

字符串插值用更加简单的方式替代了string.Format:

string s = $"It is {
      
      DateTime.Now}";
Console.WriteLine(s);

异常过滤器,可以在catch上再加一个条件:

string html;
try
{
    
    
    html = new WebClient().DownloadString("http://asef");
}
catch(WebException ex) when(ex.Status == WebExceptionStatus.Timeout)
{
    
    

}

using static 指令可以引入一个类型的所有静态成员,这样就可以不用写类型而直接使用这些成员:

using static System.Console;
WriteLine("Hello world");

nameof运算符返回变量、类型或者其他符号的名称,这样可以避免变量重命名造成不一致的代码:

int capacity = 123;
string x = nameof(capacity);
Console.WriteLine(x);

值得一提的是C# 6.0可以在catch和finally块中使用 await.

8.3 C# 5.0新特性

async和await,支持异步功能,有利于编写高并发和高效的I/O密集型应用程序。

8.4 C# 4.0新特性

1.动态绑定
2.可选参数和命名参数
3.用泛型接口和委托实现类型变化
4.改进COM互操作性

8.5 C# 3.0新特性

隐式局部变量、匿名类型、对象构造器、lambda表达式、扩展方法、查询表达式、表达式数。

8.5 C# 2.0新特性

泛型、可空类型、匿名方法、分部类、静态类

下一篇: C#核心笔记——(二)C#语言基础

猜你喜欢

转载自blog.csdn.net/weixin_45724919/article/details/134033750