net 框架

  1. dotnet --version # 版本信息
  2. dotnet new console -o <project-name> # 创建一个新的控制台应用
  3. dotnet run # 编译并运行一个C#程序

数据类型

值类型:基本类型(字符串除外),枚举,元组和结构
引用类型:类,字符串,接口,数组和委托

字符串

字符串可变性
  • System.String:不可变的字符序列
  • System.Text.StringBuilder:变的字符序列 ```csharp using System; char[] cdb = { ‘M’, ‘y’, ‘S’, ‘q’, ‘l’ }; string db = new string(cdb); // MySql

string lang = “C#”; // C# String ide = “NetBeans”; // NetBeans

  1. ```csharp
  2. using System.Text;
  3. string lang = "C#";
  4. StringBuilder sb1 = new StringBuilder(lang); // C#
  5. StringBuilder sb2 = new StringBuilder(); // Fields of glory
  6. sb2.Append("Fields");
  7. sb2.Append(" of ");
  8. sb2.Append("glory");
  9. sb.Remove(sb.Length-1, 1);
  10. sb.Insert(3, ' ');
  11. sb.Replace('M', 'm', 4, 1);

字符串是对象

对象必须具有一个类名,一个父类,并且还必须具有可以调用的方法或要访问的属性。

  1. using System;
  2. string lang = "Java";
  3. string bclass = lang.GetType().Name; // String
  4. string parclass = lang.GetType().BaseType.Name; // Object
  5. var isEmpty = lang.Equals(String.Empty); // false
  6. int len = lang.Length; // 4

连接字符串
  1. "Return" + " of " + "the king.";
  2. string.Concat(string.Concat("Return", " of ");
  3. StringBuilder sb = new StringBuilder();
  4. sb.Append("Return");
  5. sb.Append(" of ");
  6. sb.Append("the king.");
  7. var items = new string[] { "C#", "Visual Basic", "Java", "Perl" };
  8. string langs = string.Join(",", items); // C#,Visual Basic,Java,Perl
  9. string[] langs2 = langs.Split(','); // [C#,Visual Basic,Java,Perl]

转义
  1. using System;
  2. string s1 = "deep \t forest"; // deep forest

不转义
  1. using System;
  2. Console.WriteLine(@"deep \t forest"); // deep \t forest

字符串格式化string.Format
  1. using System;
  2. int age = 34;
  3. string name = "William";
  4. string msg = string.Format("{0} is {1} years old.", name, age); // William is 34 years old.

字符串插值$
  1. using System;
  2. string name = "Peter";
  3. int age = 34;
  4. string msg = $"{name} is {age} years old"; // Peter is 34 years old

常用方法
  1. using System;
  2. string word = "Determination";
  3. Console.WriteLine(word.Contains("e")); // 包含特定字符
  4. Console.WriteLine(word.IndexOf("e")); // 返回字符串中字母的第一个索引
  5. Console.WriteLine(word.LastIndexOf("i")); // 返回字符串中字母的最后一个索引
  6. Console.WriteLine(word.ToUpper()); // 转换为大写
  7. Console.WriteLine(word.ToLower()); // 转换为小写
  8. string cloned = (string) str.Clone(); // ReferenceEquals(str, cloned) = true
  9. string copied = string.Copy(str); // ReferenceEquals(str, cloned) = false

数组

  1. int[] array = new int[] {2, 4, 5, 6, 7, 3, 2 }; // 初始化
  2. int[] vals2 = vals[1..5]; // 数组切片,[4, 5, 6, 7, 3]
  3. int[] vals3 = vals[..6]; // [2, 4, 5, 6, 7, 3, 2 ]
  4. int[] vals4 = vals[3..]; // [6, 7, 3, 2]
  5. for (int i=0; i < array.Length; i++) {} // 数组遍历
  6. foreach (string planet in array) {}
  7. names.GetValue(1);
  8. names.SetValue("Beky", 1);
  9. Array.Sort(names);
  10. Array.Reverse(names);
  11. Array.IndexOf(names, "Erzebeth");
  12. Array.Copy(names, girls, names.Length);
  13. Array.Clear(names, 0, 2);

字符

  1. using System;
  2. string word = "ZetCode";
  3. char c = word[0]; // Z

元组

  1. using System;
  2. var words = ("sky", "blue", "rock", "fountain");
  3. Console.WriteLine(words.Item1); // sky
  4. Console.WriteLine(words.Item2); // blue

DateTime

  1. using System;
  2. DateTime now = DateTime.Now; // 12/5/2018 8:09:56 PM
  3. DateTime now1 = DateTime.Now.ToShortDateString(); // 12/5/2018
  4. DateTime now2 = DateTime.Now.ToShortTimeString(); // 8:09 PM

可空类型?

  1. using System;
  2. Nullable<bool> male = null;
  3. int? age = null;

类型转换()

  1. using System;
  2. double b = 13.5;
  3. float a = (float) b; // 将double值转换为float值

值转换Convert

  1. using System;
  2. // 将double值转换为bool值
  3. Console.WriteLine(Convert.ToBoolean(0.3));
  4. Console.WriteLine(Convert.ToBoolean(3));
  5. Console.WriteLine(Convert.ToBoolean(0));
  6. Console.WriteLine(Convert.ToBoolean(-1));
  7. Console.WriteLine(Convert.ToInt32("452"));
  8. Console.WriteLine(Convert.ToInt32(34.5));
  9. // 将string转换为int值
  10. Console.WriteLine(Int32.Parse("34"));
  11. Console.WriteLine(Int32.Parse("-34"));
  12. Console.WriteLine(Int32.Parse("+34"));

C# 运算符

类别 符号
符号运算符 + -
算术 + - * / %
逻辑(布尔和按位) & | ^ ! ~ && || true false
字符串连接 +
递增,递减 ++ —
移位 << >>
关系 == != < > <= >=
辅助 = += -= *= /= %= &= |= ^= ??= <<= >>=
成员访问 . ?.
索引 [] ?[]
调用 ()
三元 ?:
委托连接和删除 + -
对象创建 new
类型信息 as is sizeof typeof
异常控制 checked unchecked
间接地址 * -> [] &
Lambda =>

控制台的值Console.ReadLine

  1. using System;
  2. Console.WriteLine("This is C#"); // 等同于System.Console.WriteLine("This is C#");
  3. string name = Console.ReadLine(); // 从控制台读取一个值

数组定义List

  1. using System.Collections.Generic;
  2. var words = new List<string> { "stone", "rock", "falcon", "sky" };

解构丢弃_

  1. using System;
  2. var vals = (1, 2, 3, 4, 5, 6);
  3. (int x, int y, int z, _, _, _) = vals;

生成指定范围内的数字序列Enumerable.Range

  1. using System.Linq;
  2. var vals = Enumerable.Range(1, 6); // [1,2,3,4,5,6]

随机数random

  1. using System;
  2. Random random = new Random();