{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/csharp-misc-tips/",
  "atlas": "https://www.pystone.net/?node=csharp-misc-tips#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/csharp-misc-tips.md",
  "context": "https://www.pystone.net/notes/csharp-misc-tips.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:csharp-misc-tips",
  "slug": "csharp-misc-tips",
  "title": "CSharp零碎知识点",
  "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": "CSharp零碎知识点",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "200da633b13601e47d3dafba9530013b51d97db741e431fa2efde5fc38de5e3a",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "CSharp零碎知识点",
      "anchor": "csharp零碎知识点",
      "citation": "https://www.pystone.net/notes/csharp-misc-tips/#csharp%E9%9B%B6%E7%A2%8E%E7%9F%A5%E8%AF%86%E7%82%B9"
    },
    {
      "depth": 2,
      "text": "params",
      "anchor": "params",
      "citation": "https://www.pystone.net/notes/csharp-misc-tips/#params"
    },
    {
      "depth": 2,
      "text": "字符串前加@",
      "anchor": "字符串前加",
      "citation": "https://www.pystone.net/notes/csharp-misc-tips/#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%89%8D%E5%8A%A0"
    },
    {
      "depth": 2,
      "text": "Conditional 条件方法",
      "anchor": "conditional-条件方法",
      "citation": "https://www.pystone.net/notes/csharp-misc-tips/#conditional-%E6%9D%A1%E4%BB%B6%E6%96%B9%E6%B3%95"
    },
    {
      "depth": 2,
      "text": "where",
      "anchor": "where",
      "citation": "https://www.pystone.net/notes/csharp-misc-tips/#where"
    },
    {
      "depth": 3,
      "text": "接口约束",
      "anchor": "接口约束",
      "citation": "https://www.pystone.net/notes/csharp-misc-tips/#%E6%8E%A5%E5%8F%A3%E7%BA%A6%E6%9D%9F"
    },
    {
      "depth": 3,
      "text": "构造函数约束",
      "anchor": "构造函数约束",
      "citation": "https://www.pystone.net/notes/csharp-misc-tips/#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E7%BA%A6%E6%9D%9F"
    },
    {
      "depth": 3,
      "text": "泛型方法与委托",
      "anchor": "泛型方法与委托",
      "citation": "https://www.pystone.net/notes/csharp-misc-tips/#%E6%B3%9B%E5%9E%8B%E6%96%B9%E6%B3%95%E4%B8%8E%E5%A7%94%E6%89%98"
    },
    {
      "depth": 2,
      "text": "ref和out的区别",
      "anchor": "ref和out的区别",
      "citation": "https://www.pystone.net/notes/csharp-misc-tips/#ref%E5%92%8Cout%E7%9A%84%E5%8C%BA%E5%88%AB"
    }
  ],
  "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中的“CSharp零碎知识点”导航项",
      "citation": "https://www.pystone.net/notes/programming-languages-and-runtimes/#net%E4%B8%8Ecsharp"
    }
  ],
  "contentMarkdown": "# CSharp零碎知识点\n\n﻿# CSharp零碎知识点\n\n> 创建时间：2020/6/8 10:59\n\n## params\n\n  1. 使用 params关键字可以指定一个方法参数,该方法参数的数目可变。\n  2. 可以发送参数声明中所指定类型的逗号分隔的参数列表或指定类型的参数数组。 还可以不发送参数。 如果未发送任何参数，则 params 列表的长度为零\n  3. 在方法声明中的 params 关键字之后不允许任何其他参数，并且在方法声明中只允许一个 params 关键字。\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n## 字符串前加@\n\n@\"C:\\Program Files\\Tencent\\Tencent.exe \"\n\n在C#中”\\“是转义符，所以当写\"c:\\xxx\"时，C#会把”\\“当成转义符。\n加上@，C#就会将该字符串当作路径来处理，认成\"c:\\dos\"\n\n## Conditional 条件方法\n\n利用 Conditional 属性，程序员可以定义条件方法。Conditional 属性通过测试条件编译符号来确定适用的条件。当运行到一个条件方法调用时，是否执行该调用，要根据出现该调用时是否已定义了此符号来确定。如果定义了此符号，则执行该调用；否则省略该调用（包括对调用的参数的计算）。\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n不论如何，该条件方法会被编译。\n而在调用该方法的时候，如果相应的脚本定义了DEBUG`#define DEBUG`，则会执行TestConditional条件方法，如果没有定义，就不会执行。\n\n## where\n\nwhere的用法where 子句用于指定类型约束，这些约束可以作为泛型声明中定义的类型参数的变量。\n\n### 接口约束\n\npublic class MyGenericClass<T> where T:IComparable { }\n\n基类约束指出某个类型必须将指定的类作为基类（或者就是该类本身），才能用作该泛型类型的类型参数。 这样的约束一经使用，就必须出现在该类型参数的所有其他约束之前。\n\nclass MyClassy<T, U>\nwhere T : class\nwhere U : struct\n{\n\n}\n\n### 构造函数约束\n\n可以使用 new 运算符创建类型参数的实例；但类型参数为此必须受构造函数约束 new() 的约束。new() 约束可以让编译器知道：提供的任何类型参数都必须具有可访问的无参数（或默认）构造函数。\nnew() 约束出现在 where 子句的最后。\npublic class MyGenericClass <T> where T: IComparable, new()\n{\n// The following line is not possible without new() constraint:\nT item = new T();\n}\n\n### 泛型方法与委托\n\npublic bool MyMethod<T>(T t) where T : IMyInterface { }\ndelegate T MyDelegate<T>() where T : new()\n\n## ref和out的区别\n\n  1. 两者都是按地址传递的，使用后都将改变知原来参数的数值。\n  2. ref可以把参数的数值传递进函数，但是out是要把参数清空，就是说你无法把一个数值从out传递进去的道，out进去后，参数的数值为空，所以你必须初始化一次。这个就是两个的区别，或者说就像有的网友说的，ref是有进有出，out是只出不进。\n  3. 使用ref型参数时，传入的参数必须先被初始版化。out，必须在方法中对其完成初始化。\n  4. 使用ref和out时，在方法的参数和执行方法时，都要加Ref或Out关键字。以满足匹配。\n  5. out适合用在需要retrun多个返回值的地方，而ref则用在需要被调用的方法修改调用者的引用的时候。\n"
}
