{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/csharp-fundamentals/",
  "atlas": "https://www.pystone.net/?node=csharp-fundamentals#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/csharp-fundamentals.md",
  "context": "https://www.pystone.net/notes/csharp-fundamentals.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:csharp-fundamentals",
  "slug": "csharp-fundamentals",
  "title": "C#基础知识",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "C 基础知识",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "4b6297e6d08fab411f9feb5befec68a5458ed3246befb6a35b196a173a2b67e3",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "C#基础知识",
      "anchor": "c基础知识",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#c%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"
    },
    {
      "depth": 3,
      "text": "类",
      "anchor": "类",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E7%B1%BB"
    },
    {
      "depth": 4,
      "text": "类的意义",
      "anchor": "类的意义",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E7%B1%BB%E7%9A%84%E6%84%8F%E4%B9%89"
    },
    {
      "depth": 4,
      "text": "面向对象",
      "anchor": "面向对象",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"
    },
    {
      "depth": 4,
      "text": "值类型和引用类型",
      "anchor": "值类型和引用类型",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E5%80%BC%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B"
    },
    {
      "depth": 4,
      "text": "类的访问权限",
      "anchor": "类的访问权限",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E7%B1%BB%E7%9A%84%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90"
    },
    {
      "depth": 4,
      "text": "抽象类与接口",
      "anchor": "抽象类与接口",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E6%8A%BD%E8%B1%A1%E7%B1%BB%E4%B8%8E%E6%8E%A5%E5%8F%A3"
    },
    {
      "depth": 4,
      "text": "内部类",
      "anchor": "内部类",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E5%86%85%E9%83%A8%E7%B1%BB"
    },
    {
      "depth": 4,
      "text": "局部类型（Partial）",
      "anchor": "局部类型partial",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E5%B1%80%E9%83%A8%E7%B1%BB%E5%9E%8Bpartial"
    },
    {
      "depth": 4,
      "text": "泛型",
      "anchor": "泛型",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E6%B3%9B%E5%9E%8B"
    },
    {
      "depth": 4,
      "text": "字段，属性，成员变量",
      "anchor": "字段属性成员变量",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E5%AD%97%E6%AE%B5%E5%B1%9E%E6%80%A7%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F"
    },
    {
      "depth": 3,
      "text": "结构体",
      "anchor": "结构体",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E7%BB%93%E6%9E%84%E4%BD%93"
    },
    {
      "depth": 3,
      "text": "深拷贝与浅拷贝",
      "anchor": "深拷贝与浅拷贝",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E6%B7%B1%E6%8B%B7%E8%B4%9D%E4%B8%8E%E6%B5%85%E6%8B%B7%E8%B4%9D"
    },
    {
      "depth": 3,
      "text": "接口",
      "anchor": "接口",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E6%8E%A5%E5%8F%A3"
    },
    {
      "depth": 4,
      "text": "隐式实现",
      "anchor": "隐式实现",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E9%9A%90%E5%BC%8F%E5%AE%9E%E7%8E%B0"
    },
    {
      "depth": 4,
      "text": "显式实现",
      "anchor": "显式实现",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E6%98%BE%E5%BC%8F%E5%AE%9E%E7%8E%B0"
    },
    {
      "depth": 3,
      "text": "委托",
      "anchor": "委托",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E5%A7%94%E6%89%98"
    },
    {
      "depth": 3,
      "text": "事件",
      "anchor": "事件",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E4%BA%8B%E4%BB%B6"
    },
    {
      "depth": 3,
      "text": "属性器",
      "anchor": "属性器",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E5%B1%9E%E6%80%A7%E5%99%A8"
    },
    {
      "depth": 3,
      "text": "表达式",
      "anchor": "表达式",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E8%A1%A8%E8%BE%BE%E5%BC%8F"
    },
    {
      "depth": 4,
      "text": "数值和字符串",
      "anchor": "数值和字符串",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E6%95%B0%E5%80%BC%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2"
    },
    {
      "depth": 4,
      "text": "调用表达式",
      "anchor": "调用表达式",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E8%B0%83%E7%94%A8%E8%A1%A8%E8%BE%BE%E5%BC%8F"
    },
    {
      "depth": 4,
      "text": "查询表达式",
      "anchor": "查询表达式",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E6%9F%A5%E8%AF%A2%E8%A1%A8%E8%BE%BE%E5%BC%8F"
    },
    {
      "depth": 4,
      "text": "Lambda表达式",
      "anchor": "lambda表达式",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F"
    },
    {
      "depth": 4,
      "text": "表达式树",
      "anchor": "表达式树",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%A0%91"
    },
    {
      "depth": 4,
      "text": "表达式主体定义：Expression-bodied members",
      "anchor": "表达式主体定义expression-bodied-members",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%B8%BB%E4%BD%93%E5%AE%9A%E4%B9%89expression-bodied-members"
    },
    {
      "depth": 3,
      "text": "语句",
      "anchor": "语句",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E8%AF%AD%E5%8F%A5"
    },
    {
      "depth": 3,
      "text": "Attribute(特性，属性）",
      "anchor": "attribute特性属性",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#attribute%E7%89%B9%E6%80%A7%E5%B1%9E%E6%80%A7"
    },
    {
      "depth": 5,
      "text": "SerializableAttribute",
      "anchor": "serializableattribute",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#serializableattribute"
    },
    {
      "depth": 5,
      "text": "SerializeField",
      "anchor": "serializefield",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#serializefield"
    },
    {
      "depth": 5,
      "text": "自定义Attribute",
      "anchor": "自定义attribute",
      "citation": "https://www.pystone.net/notes/csharp-fundamentals/#%E8%87%AA%E5%AE%9A%E4%B9%89attribute"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:programming-languages-and-runtimes",
      "title": "编程语言与运行时",
      "url": "https://www.pystone.net/notes/programming-languages-and-runtimes/",
      "atlas": "https://www.pystone.net/?node=programming-languages-and-runtimes#knowledge-atlas",
      "label": "编程语言与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": ".NET与CSharp中的“C#基础知识”导航项",
      "citation": "https://www.pystone.net/notes/programming-languages-and-runtimes/#net%E4%B8%8Ecsharp"
    }
  ],
  "contentMarkdown": "# C#基础知识\n\n> 创建时间：2020/4/20 16:34\n\n* C# 1.0\n    * 类\n      * 类的意义\n      * 面向对象\n      * 值类型和引用类型\n      * 类的访问权限\n      * 抽象类与接口\n      * 内部类\n      * 局部类型（Partial）\n      * 泛型\n      * 字段，属性，成员变量\n    * 结构体\n    * 深拷贝与浅拷贝\n    * 接口\n      * 隐式实现\n      * 显式实现\n    * 委托\n    * 事件\n    * 属性器\n    * 表达式\n      * 数值和字符串\n      * 调用表达式\n      * 查询表达式\n      * Lambda表达式\n      * 表达式树\n      * 表达式主体定义：Expression-bodied members\n    * 语句\n    * Attribute(特性，属性）\n      * SerializableAttribute\n      * SerializeField\n      * 自定义Attribute\n\n>   * 类（Class）\n>   * 结构（Struct）\n>   * 接口（Interface）\n>   * 事件（Event）\n>   * 属性（Property）\n>   * 委托（Delegates）\n>   * 表达式\n>   * 语句\n>   * 特性（属性）（Attribute）\n>\n\n### 类\n\n>   * 包含变量（属性）\n>   * 包含方法（行为）\n>   * 继承\n>   * 用来创建对象\n>\n\n#### 类的意义\n\n继承功能，让用户可以更方便地服用代码，更方便地使用引擎\nUnity脚本继承MonoBehaviour类。这个基类提供了丰富的功能和API\n\n#### 面向对象\n\n面向对象，可以理解为建模\n\n#### 值类型和引用类型\n\n**值类型（value type）** ：直接存储数据。\nbyte，short，int，bong，float，double，decimal，char，bool，struct。变量声明后，不管是否已经赋值，编译器为其分配内存。\n**引用类型（reference type）** ：存储数据的引用，数据存储在堆中。\nstring和class。声明一个类时，只在栈中分配一小块儿内存用于容纳一个地址，此时并没有为其分配堆上的内存空间。当使用 new 创建一个类的实例时，分配堆上的空间，并把堆上空间的地址保存到栈上分配的小片空间中。\n\n#### 类的访问权限\n\n  * Internal：打DLL的时候，控制有些类不让用户访问到\n  * Private\n  * Public\n\n#### 抽象类与接口\n\n#### 内部类\n\n类内部，只需要在类内部使用的类\n\n#### 局部类型（Partial）\n\nhttps://www.cnblogs.com/w-wfy/p/7206294.html\n局部类型允许我们将一个类、结构或接口分成几个部分，分别实现在几个不同的.cs文件中。\n\n> 是一个纯语言层的编译处理，编译器在编译的时候仍会将各个部分的局部类型合并成一个完整的类\n\n**适用于**\n（1）类型特别大，不宜放在一个文件中实现\n（2） 一个类型中的一部分代码为自动化工具生成的代码，不宜与我们自己编写的代码混合在一起。\n（3）需要多人合作编写一个类。\n**注意点**\n（1）局部类型只适用于类、接口、结构，不支持委托和枚举。\n（2）同一个类型的各个部分必须都有修饰符 partial。\n（3）使用局部类型时，一个类型的各个部分必须位于相同的命名空间中。\n（4）一个类型的各个部分必须被同时编译。\n（5）关键字partial是一个上下文关键字，只有和 class、struct、interface 放在一起时才有关键字的含义。因此partial的引入不会影响现有代码中名称为partial的变量。\n（6)各个部分一般是分开放在几个不同的.cs文件中，但C#编译器允许我们将他们放在同一文件中。\n\n**“累加”效应**\n\n* * *\n\n[Attribute1, Attribute2(\"Hello\")]\npartial class Class1{}\n\n[Attribute3, Attribute2(\"Exit\")]\npartial class Class1{}\n相当于\n\n[Attribute1, Attribute2(\"Hello\"), Attribute3, Attribute2(\"Exit\")]\nclass Class1 {}\n\n* * *\n\npartial class Class2: Iinterface1, Iinterface2 {}\npartial class Class2: Iinterface3 {}\npartial class Class2: Iinterface2 {}\n相当于\nclass Class2: Iinterface1, Iinterface2, Iinterface3 {}\n\n**修饰符**\n（1）各个部分上的访问修饰符必须维持一致性，不能使用相互矛盾的修饰符。\n（2）如果一个部分类使用了abstract/sealed/static修饰符，那么整个类都将被视为抽象类/密封类/静态类。\n\n#### 泛型\n\nhttps://www.cnblogs.com/arxive/p/6179972.html\nhttps://blog.csdn.net/kebi007/article/details/77800954\n\n> **泛型** ：通过 **参数化类型** 来实现在同一份代码上操作多种数据类型。\n>  **泛型编程** 是一种编程范式，它利用“参数化类型”将类型抽象化，从而以一种 **独立于任何特定类型** 的方式编写代码，实现更为灵活的复用。\n\n  1. 泛型方法\n  2. 泛型类\n  3. 泛型集合\n  4. 泛型委托\n  5. 泛型约束\n\n#### 字段，属性，成员变量\n\n**字段（成员变量）** ：理解为一般意义上的变量。一般声明为private类型\n**属性** ：是类展示给外部使用者的特性。由特殊方法（Get 和 Set 访问器）使得外部类访问当前类的字段。Get 和 Set 访问器允许在设置或检索属性之后验证属性值、执行其他代码或执行其他任务。\n\n> 面向对象的编程方法是要 **抽象、封装**\n>  **字段（Field)** 可以为public或private，若把字段声明为public，则外部使用者可以对其值进行直接修改，不利于类的封装。\n>\n>   1. **属性** 如何对其进行赋值（ **set** ）如何进行取值（ **get** ）都已经进行了封装， **如何控制操作是由类自身决定** 的。防止外部用户随意更改类成员变量的值, 做到安全限制，其优点是 **数据安全**\n>   2. **属性** 可用来 **隔离** 类内部的 **变化** ，体现出 **封装性** 。（见下文:属性器）\n>\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n> 字段与属性的特性区别\n>\n>   * 字段总是可读可写(readonlykeyword除外)属性可读可写(至少要有一样)\n>   * 字段总是马上运行。效率高。属性还须要调用方法，效率低\n>   * 字段能作为ref,out參数。属性不能够\n>\n\n> **字段** 和 **属性** 是相对于类而言的，而 **变量** 相对于方法或者语句块而言\n\n### 结构体\n\n**Struct** 是值类型，作为一个属性时，Struct无法对单一的成员变量赋值，只能对整个Struct赋值。\n**Class** 则只要允许，即可随便赋值\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n> Sturct关键点：\n>\n>   * 是值类型\n>   * 不能设置为null，且不能向结构变量分配null，除非将变量声明为可为空的值类型。？？？\n>   * 声明变量时，本身就有值了\n>   * 赋值时是深拷贝（将结构体分配给新变量时，将复制所有数据，并且对新副本所做的任何修改不会影响原始副本的数据。处理值类型的集合（Dictionary<string, myStruct>）时务必记住这一点。\n>   * 不能局部赋值（transform.position.x不能直接赋值）？？？\n>\n\n> 其他细节：\n>\n>   * 结构体声明中，除非声明为const或static，否则无法初始化\n>   * 不能声明无参构造函数或终结器\n>   * 与类不同，无需使用new运算符进行实例化。\n>   * 结构体无法继承自另一个结构或类，并且不能作为类的基类。所有的结构体都继承自ValueType，ValueType继承自Object\n>   * 结构体可以实现接口\n>\n\n### 深拷贝与浅拷贝\n\n**浅拷贝** 是指将对象中的数值类型的字段拷贝到新的对象中，而对象中的 **引用** 型字段则指 **复制它的一个引用到目标对象** 。如果改变目标对象中引用型字段的值他将反映在原始对象中，也就是说原始对象中对应的字段也会发生变化。\n\n**深拷贝** 对于 **引用** ，将会在新对象中 **创建一个新** 的和原始对象中对应字段相同（内容相同）的 **字段** ，也就是说这个引用和原始对象的引用是 **不同的** ，我们在改变新对象中的这个字段的时候是 **不会影响到原始对象中对应字段** 的内容。\n\n.net提供了一个 **ICloneable接口** ，Clone()方法，你可以实现它用来实现你自己的克隆方式。\nMemberwiseClone()是object类中的一个方法，用来实现类的浅克隆\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n### 接口\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### 隐式实现\n\n```csharp\nstring str1 = String.Empty;\nstring str2 = String.Empty;\n\nstr2 = String.Intern(sb.ToString());\n\nif((object)str1==(object)str2)\n    Console.WriteLine(&quot;The strings are equal.&quot;);\nelse\n    Console.WriteLine(&quot;The strings are not equal.&quot;);\n\n```\n\n#### 显式实现\n\n```csharp\nstring s = String.Empty;\n\n```\n\n**显示实现的方法调用时，引用的类型必须是所实现的接口类型**\n\n错误：\n\n```csharp\nstring s = String.Empty;\n\n```\n\n正确：\n\n```csharp\nstring s = String.Empty;\n\n```\n\n**用法一** ：写一些系统或一些底层模块的时候，为了解决实现多个接口时可能出现方法重名的问题。\n\n```csharp\nstring s = String.Empty;\n\n```\n\n**用法二** ：降低接口方法的访问权限\n\n```csharp\nstring s = String.Empty;\n\n```\n\n用户创建出来的对象无法直接使用接口方法，而是把接口方法交给专门的调用器调用，避免用户错误操作。\n\n> Notes:\n>\n>   * 接口类似于只有抽象成员的抽象基类。实现接口的任何类或结构都必须实现其所有成员。\n>   * 接口无法直接实例化。\n>   * 接口可以包含事件，索引器，方法和属性。\n>   * 接口不包含方法的实现\n>   * 一个类只能继承一个基类，可以实现一个或多个接口\n>\n\n### 委托\n\nC# **委托** 比C++ **函数指针** 更安全，比Java的 **匿名类** 更简洁。\n内置了Action<T>, Func<T>等委托定义，一般不需要自己定义委托\n\nNotice：\n\n  * 注册委托和注销委托最好成对出现\n  * 委托有可能为null，所以最好在声明委托变量时，设置一个初始值，可以减少空指针异常的风险。如下：\n\n```csharp\nstring s = String.Empty;\n\n```\n\n### 事件\n\n事件和委托的区别： **事件只能在类的内部触发** ，只能从内向外发送事件。\n\n声明过程：\n第一步：声明委托(声明一下这个“回调函数指针列表当中的函数时什么样子的”)\n第二步：声明与上述委托相关的事件\n\n```csharp\nstring s = String.Empty;\n\n```\n\n另外，Event的属性器不是Set和Get，而是Add和Remove\n\n```csharp\nstring s = String.Empty;\n\n```\n\n### 属性器\n\n**用来隔离类内部的变化，体现出面向对象的封装性**\n学生的Age每年都要变化一次。若根据生日自动计算，就不需要在类的外部控制Age的数值（内部自动控制）。\n\n```csharp\nstring s = String.Empty;\n\n```\n\n### 表达式\n\n表达式是由一个或多个操作数以及零个或多个组成的序列，其计算结果为一个值，对象，方法或命名空间\n\n#### 数值和字符串\n\n声名两个变量，也叫两个表达式的声明\n\n```csharp\nstring s = String.Empty;\n\n```\n\n#### 调用表达式\n\n```csharp\nstring s1 = &quot;Hello &quot;;\nstring s2 = s1;\ns1 += &quot;World&quot;;\n\nSystem.Console.WriteLine(s2);\n//Output: Hello\n\n```\n\n#### 查询表达式\n\n虽然C# 1.0没有支持，但是LINQ则是查询表达式\n\n#### Lambda表达式\n\n```csharp\nstring s1 = &quot;Hello &quot;;\nstring s2 = s1;\ns1 += &quot;World&quot;;\n\nSystem.Console.WriteLine(s2);\n//Output: Hello\n\n```\n\n#### 表达式树\n\nhttps://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/expression-trees/\n\n#### 表达式主体定义：Expression-bodied members\n\nhttps://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members\n\n### 语句\n\n任何可以用分号结尾的代码就是一个语句\n类型：\n\n  * 声明语句\n  * 表达式语句\n  * 选择语句：if else switch case\n  * 迭代语句：do for foreach in while\n  * 跳转语句：break continue default goto return yield\n  * 异常处理语句： throw try-catch try-finally try-catch-finally\n  * Await语句\n  * Yield Return语句\n  * Fixed语句\n  * Lock语句\n  * 空语句\n  * 等等\n\n### Attribute(特性，属性）\n\n使用Attribute，可以在类或者方法，属性，程序集中声明一些标记，标记程序后，可以在运行时使用反射来查询特性。\n\n##### SerializableAttribute\n\n使用Serializable标记的类，可以进行序列化和反序列化，比如用JsonUtility或者XML库\n\n```csharp\nstring s1 = &quot;Hello &quot;;\nstring s2 = s1;\ns1 += &quot;World&quot;;\n\nSystem.Console.WriteLine(s2);\n//Output: Hello\n\n```\n\n##### SerializeField\n\nUnity提供，可以让私有成员变量在Inspector上显示\n\n##### 自定义Attribute\n\n继承Attribute类即可\n将定义好的Attribute标记到一个成员变量上\n往往需要和反射技术一起使用，利用反射获取对象的类型信息，从而根据标记的Attribute实现相应的功能\n\n```csharp\nstring s1 = &quot;Hello &quot;;\nstring s2 = s1;\ns1 += &quot;World&quot;;\n\nSystem.Console.WriteLine(s2);\n//Output: Hello\n\n```\n```csharp\nstring s1 = &quot;Hello &quot;;\nstring s2 = s1;\ns1 += &quot;World&quot;;\n\nSystem.Console.WriteLine(s2);\n//Output: Hello\n\n```\n"
}
