ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 基础知识 - 数据类型 bool 布尔值 True 或 False False byte 8 位无符号整数 0 到 255 0 char 16 位 Unicode 字符 U +0000 到 U +ffff '\\0' decimal 128 位精确的十进制值,28-29 有效位数 (-7.9 x 1028 到 7.9 x 1028) / 100 到 28 0.0M double 64 位双精度浮点型 (+/-)5.0 x 10-324 到 (+/-)1.7 x 10308 0.0D float 32 位单精度浮点型 -3.4 x 1038 到 + 3.4 x 1038 0.0F int 32 位有符号整数类型 -2,147,483,648 到 2,147,483,647 0 long 64 位有符号整数类型 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 0L sbyte 8 位有符号整数类型 -128 到 127 0 short 16 位有符号整数类型 -32,768 到 32,767 0 uint 32 位无符号整数类型 0 到 4,294,967,295 0 ulong 64 位无符号整数类型 0 到 18,446,744,073,709,551,615 0 ushort 16 位无符号整数类型 0 到 65,535 0 ``` <pre class="calibre14">``` double<span class="token2">[</span><span class="token2">]</span> balance <span class="token">=</span> <span class="token5">new</span> <span class="token1">double</span><span class="token2">[</span><span class="token3">10</span><span class="token2">]</span><span class="token2">;</span>声明数组十个元素 double<span class="token2">[</span><span class="token2">]</span> balance <span class="token">=</span> <span class="token2">{</span> <span class="token3">2340.0</span><span class="token2">,</span> <span class="token3">4523.69</span><span class="token2">,</span> <span class="token3">3421.0</span><span class="token2">}</span><span class="token2">;</span> int <span class="token2">[</span><span class="token2">]</span> marks <span class="token">=</span> <span class="token5">new</span> <span class="token1">int</span><span class="token2">[</span><span class="token2">]</span> <span class="token2">{</span> <span class="token3">99</span><span class="token2">,</span> <span class="token3">98</span><span class="token2">,</span> <span class="token3">92</span><span class="token2">,</span> <span class="token3">97</span><span class="token2">,</span> <span class="token3">95</span><span class="token2">}</span><span class="token2">;</span> int <span class="token2">[</span><span class="token2">]</span><span class="token2">[</span><span class="token2">]</span> scores<span class="token2">;</span> 交错数组<span class="token2">,</span>属于一维数组 string <span class="token2">[</span><span class="token2">,</span><span class="token2">]</span> names<span class="token2">;</span> 二维数组 int <span class="token2">[</span> <span class="token2">,</span> <span class="token2">,</span> <span class="token2">]</span> m<span class="token2">;</span> 三维数组 int <span class="token2">[</span><span class="token2">,</span><span class="token2">]</span> a <span class="token">=</span> <span class="token5">new</span> <span class="token1">int</span> <span class="token2">[</span><span class="token3">3</span><span class="token2">,</span><span class="token3">4</span><span class="token2">]</span> <span class="token2">{</span> <span class="token2">{</span><span class="token3">0</span><span class="token2">,</span> <span class="token3">1</span><span class="token2">,</span> <span class="token3">2</span><span class="token2">,</span> <span class="token3">3</span><span class="token2">}</span> <span class="token2">,</span> <span class="token6">/* 初始化索引号为 0 的行 */</span> <span class="token2">{</span><span class="token3">4</span><span class="token2">,</span> <span class="token3">5</span><span class="token2">,</span> <span class="token3">6</span><span class="token2">,</span> <span class="token3">7</span><span class="token2">}</span> <span class="token2">,</span> <span class="token6">/* 初始化索引号为 1 的行 */</span> <span class="token2">{</span><span class="token3">8</span><span class="token2">,</span> <span class="token3">9</span><span class="token2">,</span> <span class="token3">10</span><span class="token2">,</span> <span class="token3">11</span><span class="token2">}</span> <span class="token6">/* 初始化索引号为 2 的行 */</span> <span class="token2">}</span><span class="token2">;</span> struct Books 结构体类型 <span class="token2">{</span> public string title<span class="token2">;</span> public string author<span class="token2">;</span> public string subject<span class="token2">;</span> public int book_id<span class="token2">;</span> <span class="token2">}</span><span class="token2">;</span> enum Day <span class="token2">{</span> Sun<span class="token2">,</span> Mon<span class="token2">,</span> Tue<span class="token2">,</span> Wed<span class="token2">,</span> Thu<span class="token2">,</span> Fri<span class="token2">,</span> Sat <span class="token2">}</span><span class="token2">;</span> 枚举类型 class <span class="token1">Line</span><span class="token2">{</span> private double length<span class="token2">;</span>私有方法 public void <span class="token1">setLength</span><span class="token2">(</span> double len <span class="token2">)</span> 可访问方法 <span class="token2">{</span> length <span class="token">=</span> len<span class="token2">;</span> <span class="token2">}</span> static void <span class="token1">Main</span><span class="token2">(</span>string<span class="token2">[</span><span class="token2">]</span> args<span class="token2">)</span><span class="token2">{</span><span class="token2">}</span> 静态方法 <span class="token2">}</span> class <span class="token1">A</span><span class="token2">:</span>Line<span class="token2">{</span><span class="token2">}</span> 继承类 interface <span class="token1">IMyInterface</span> 接口 <span class="token2">{</span> <span class="token6">// 接口成员</span> void <span class="token1">MethodToImplement</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> namespace namespace_name 命名空间 <span class="token2">{</span> <span class="token6">// 代码声明</span> <span class="token2">}</span> object obj<span class="token2">;</span> 对象类型 dynamic d <span class="token">=</span> <span class="token3">20</span><span class="token2">;</span> 动态类型<span class="token2">,</span>自动推断<span class="token2">,</span>可以是任何类型 String str <span class="token">=</span> <span class="token4">"runoob.com"</span><span class="token2">;</span> 字符串类型 string str <span class="token">=</span> @<span class="token4">"C:\Windows"</span><span class="token2">;</span> 不会转义 type<span class="token">*</span> identifier<span class="token2">;</span> 指针类型 double d <span class="token">=</span> <span class="token3">5673.74</span><span class="token2">;</span>d<span class="token">=</span> <span class="token2">(</span>int<span class="token2">)</span>d<span class="token2">;</span> 类型转换 方法转换如下 <span class="token3">1</span> ToBoolean 如果可能的话,把类型转换为布尔型。 <span class="token3">2</span> ToByte 把类型转换为字节类型。 <span class="token3">3</span> ToChar 如果可能的话,把类型转换为单个 Unicode 字符类型。 <span class="token3">4</span> ToDateTime 把类型(整数或字符串类型)转换为 日期<span class="token">-</span>时间 结构。 <span class="token3">5</span> ToDecimal 把浮点型或整数类型转换为十进制类型。 <span class="token3">6</span> ToDouble 把类型转换为双精度浮点型。 <span class="token3">7</span> ToInt16 把类型转换为 <span class="token3">16</span> 位整数类型。 <span class="token3">8</span> ToInt32 把类型转换为 <span class="token3">32</span> 位整数类型。 <span class="token3">9</span> ToInt64 把类型转换为 <span class="token3">64</span> 位整数类型。 <span class="token3">10</span> ToSbyte 把类型转换为有符号字节类型。 <span class="token3">11</span> ToSingle 把类型转换为小浮点数类型。 <span class="token3">12</span> ToString 把类型转换为字符串类型。 <span class="token3">13</span> ToType 把类型转换为指定类型。 <span class="token3">14</span> ToUInt16 把类型转换为 <span class="token3">16</span> 位无符号整数类型。 <span class="token3">15</span> ToUInt32 把类型转换为 <span class="token3">32</span> 位无符号整数类型。 <span class="token3">16</span> ToUInt64 把类型转换为 <span class="token3">64</span> 位无符号整数类型。 ``` ``` - 变量和常量 ``` <pre class="calibre14">``` int d <span class="token">=</span> <span class="token3">3</span><span class="token2">,</span> f <span class="token">=</span> <span class="token3">5</span><span class="token2">;</span> const int c1 <span class="token">=</span> <span class="token3">5</span><span class="token2">;</span> 常量 const string a <span class="token">=</span> @<span class="token4">"中国人"</span><span class="token2">;</span> ``` ``` - 运算符 ``` <pre class="calibre14">``` <span class="token">+</span> <span class="token">-</span> <span class="token">*</span> <span class="token">/</span> <span class="token">++</span> <span class="token">--</span> <span class="token">%</span> <span class="token">==</span> <span class="token">!=</span> <span class="token">></span> <span class="token">>=</span> <span class="token"><</span> <span class="token"><=</span> <span class="token">+</span><span class="token">=</span> <span class="token">-</span><span class="token">=</span> <span class="token">*</span><span class="token">=</span> <span class="token">/</span><span class="token">=</span> <span class="token">?</span><span class="token2">:</span> <span class="token">&&</span> 与 <span class="token">||</span> 或 <span class="token">!</span>非 <span class="token1">sizeof</span><span class="token2">(</span><span class="token2">)</span> 数据类型 <span class="token1">typeof</span><span class="token2">(</span><span class="token2">)</span> class类型 <span class="token">&</span>a 变量地址 <span class="token">*</span>a 变量的指针 num3 <span class="token">=</span> num1 <span class="token">?</span><span class="token">?</span> <span class="token3">5.34</span><span class="token2">;</span> 值为<span class="token5">null</span>时候取值<span class="token2">,</span>类似php ``` ``` - 条件语句 ``` <pre class="calibre14">``` <span class="token5">if</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> <span class="token2">}</span><span class="token5">else</span> <span class="token5">if</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> <span class="token2">}</span> switch <span class="token2">(</span>grade<span class="token2">)</span> <span class="token2">{</span> case <span class="token4">'A'</span><span class="token2">:</span> Console<span class="token2">.</span><span class="token1">WriteLine</span><span class="token2">(</span><span class="token4">"很棒!"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> case <span class="token4">'B'</span><span class="token2">:</span> case <span class="token4">'C'</span><span class="token2">:</span> Console<span class="token2">.</span><span class="token1">WriteLine</span><span class="token2">(</span><span class="token4">"做得好"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> case <span class="token4">'D'</span><span class="token2">:</span> Console<span class="token2">.</span><span class="token1">WriteLine</span><span class="token2">(</span><span class="token4">"您通过了"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> case <span class="token4">'F'</span><span class="token2">:</span> Console<span class="token2">.</span><span class="token1">WriteLine</span><span class="token2">(</span><span class="token4">"最好再试一下"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> default<span class="token2">:</span> Console<span class="token2">.</span><span class="token1">WriteLine</span><span class="token2">(</span><span class="token4">"无效的成绩"</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` - 循环 continue跳出当前执行下一条,break;退出 ``` <pre class="calibre16">``` <span class="token5">while</span><span class="token2">(</span>condition<span class="token2">)</span> <span class="token2">{</span> <span class="token2">}</span> <span class="token5">for</span> <span class="token2">(</span>int a <span class="token">=</span> <span class="token3">10</span><span class="token2">;</span> a <span class="token"><</span> <span class="token3">20</span><span class="token2">;</span> a <span class="token">=</span> a <span class="token">+</span> <span class="token3">1</span><span class="token2">)</span> <span class="token2">{</span> Console<span class="token2">.</span><span class="token1">WriteLine</span><span class="token2">(</span><span class="token4">"a 的值: {0}"</span><span class="token2">,</span> a<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> foreach <span class="token2">(</span>int element <span class="token5">in</span> fibarray<span class="token2">)</span> <span class="token6">//数组或对象</span> <span class="token2">{</span> <span class="token2">}</span> ``` ```