读书笔记: C# 7.0 in a nutshell (一、二章)

编程入门 行业动态 更新时间:2024-10-09 16:23:09

<a href=https://www.elefans.com/category/jswz/34/1768764.html style=读书笔记: C# 7.0 in a nutshell (一、二章)"/>

读书笔记: C# 7.0 in a nutshell (一、二章)

内容:

第一章:

  • 内存管理
  • IL
  • 框架
  • C# 7和6的特性

第二章: C#基础

  • 编译
  • 避免keyword冲突
  • 类型
  • 数字类型
  • Bool
  • String
  • Array
  • 变量和参数Null
  • Statement
  • Namespace

第一章 介绍C#和 .NET Framework

内存管理

C# 依赖runtime来执行自动内存管理。比如.NET Framework中的 Common Language Runtime(CLR)。 同时C#也没有完全不允许使用指针, 对于性能要求高的地方,也可以使用unsafe来表示自己进行内存管理。

IL

C#语言的 managed code 叫做Intermediate Language(IL) , CLR通常是在执行前,将IL转换成native machien code,这叫做 Just-In-Time(JIT)编译。 也有提前编译用来增加性能。

IT是被叫做 assembly包含的, assembly可以是一个 .exe 也可以是一个dll。 assembly包含IL还有其他的 metadata

框架

  1. .NET Framework
  2. Universal Windows Platform(UWP)
  3. .NET Core + ASP.NET Core
  4. Xamarin

四个框架的主要区别他们支持的平台不同、上层library不同,还有使用目的不同。

近期版本语言新增特点

C# 7.0

1.数字字面量

int million = 1_000_000;   // 数字中间加上 下划线增加可读性var b = 0b1010_1011_1100_1101_1110_1111;   // 0b 开头表示二进制

2.Out变量创建和丢弃

bool successful = int.TryParse ("123", out int result);   //不需要提前创建变量来使用outSomeBigMethod (out _, out _, out _, out int x, out _, out _, out _);  //对于不关心的值,可以使用 _ 来丢弃

3.Pattern Variable

void Foo (object x)
{if (x is string s)              // 使用is,通过判断来创建变量Console.WriteLine (s.Length);
}switch (x)
{case int i:Console.WriteLine ("It's an int!");break;case string s:Console.WriteLine (s.Length); // We can use the s variablebreak;case bool b when b == true: // Matches only when b is trueConsole.WriteLine ("True");break;case null:                              // 可以和null比较Console.WriteLine ("Nothing");break;
}

4.local methods

void WriteCubes()
{Console.WriteLine (Cube (3));Console.WriteLine (Cube (4));Console.WriteLine (Cube (5));int Cube (int value) => value * value * value;   //在方法中创建方法
}

5.箭头语法的扩展

public class Person
{
string name;
public Person (string name) => Name = name;         //构造函数
public string Name
{
get => name;
set => name = value ?? "";                                      //set的支持
}
~Person () => Console.WriteLine ("finalize");     //析构函数
}

6.Deconstructor

从类对象中抽取值

public void Deconstruct (out string firstName, out string lastName)
{int spacePos = name.IndexOf (

更多推荐

读书笔记: C# 7.0 in a nutshell (一、二章)

本文发布于:2024-02-13 21:42:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:读书笔记   二章   nutshell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!