安装 Visual Studio 2022

doc
Visual Studio IDE 入门

字符类型

  1. // 格式化输出
  2. string firstFriend = "Maria";
  3. string secondFriend = "Sage";
  4. aFriend = "Maira";
  5. Console.WriteLine("Hello " + aFriend);
  6. Console.WriteLine($"My friends are {firstFriend} and {secondFriend}");

// 清理空格 .TrimStart() .TrimEnd() .Trim()

  1. // 清理空格 .TrimStart() .TrimEnd() .Trim()
  2. string greeting = " Hello World! ";
  3. Console.WriteLine($"[{greeting}]");
  4. // [ Hello World! ]
  5. string trimmedGreeting = greeting.TrimStart();
  6. Console.WriteLine($"[{trimmedGreeting}]");
  7. // [Hello World! ] .TrimStart()
  8. trimmedGreeting = greeting.TrimEnd();
  9. Console.WriteLine($"[{trimmedGreeting}]");
  10. // [ Hello World!] .TrimEnd()
  11. trimmedGreeting = greeting.Trim();
  12. Console.WriteLine($"[{trimmedGreeting}]");
  13. // [Hello World!] .Trim()

// 字符替换.Replace()

  1. // 字符替换 .Replace()
  2. string sayHello = "Hello World!";
  3. Console.WriteLine(sayHello);
  4. sayHello = sayHello.Replace("Hello", "Greetings");
  5. Console.WriteLine(sayHello);

// 大小写转换 .ToUpper() .ToLower()

  1. // 大小写转换 .ToUpper() .ToLower()
  2. Console.WriteLine(sayHello.ToUpper());
  3. Console.WriteLine(sayHello.ToLower());

// 字符串匹配 .Contains() .StartsWith() .EndsWith()

  1. // 字符串匹配 .Contains() .StartsWith() .EndsWith()
  2. string songLyrics = "You say goodbye, and I say hello";
  3. Console.WriteLine(songLyrics.Contains("goodbye")); //True
  4. Console.WriteLine(songLyrics.Contains("greetings")); //False
  5. string songLyrics = "You say goodbye, and I say hello";
  6. Console.WriteLine(songLyrics.StartsWith("You")); //True
  7. Console.WriteLine(songLyrics.StartsWith("goodbye")); //False
  8. Console.WriteLine(songLyrics.EndsWith("hello")); //True
  9. Console.WriteLine(songLyrics.EndsWith("goodbye")); //False

// 逐字字符串文本

  1. Console.WriteLine(@" c:\source\repos
  2. (this is where your code goes)");
  3. // c:\source\repos
  4. // (this is where your code goes)

// Unicode 转义字符

  1. // Kon'nichiwa World
  2. Console.WriteLine("\u3053\u3093\u306B\u3061\u306F World!");
  3. //こんにちは World!

// 混合数字计算的 格式化输出

  1. string firstName = "Bob";
  2. int widgetsSold = 7;
  3. Console.WriteLine(firstName + " sold " + (widgetsSold + 7) + " widgets.");

数字类型 (多了一个decimal类型)

  1. //十进制类型的范围较小,但精度高于双倍。
  2. //The decimal type has a smaller range
  3. //but greater precision than double.
  4. double a = 1.0;
  5. double b = 3.0;
  6. Console.WriteLine(a / b);
  7. //0.333333333333333
  8. decimal c = 1.0M;
  9. decimal d = 3.0M;
  10. Console.WriteLine(c / d);
  11. //0.3333333333333333333333333333

注:数字上的M后缀是您表示常量应该使用十进制类型的方式。 否则,编译器假定双重类型。 字母M被选为doubledecimal关键字之间最有视觉上的字母。

列表类型

  1. var names = new List<string> { "<name>", "Ana", "Felipe" };
  2. foreach (var name in names)
  3. {
  4. Console.WriteLine($"Hello {name.ToUpper()}!");
  5. }
  6. Console.WriteLine();
  7. names.Add("Maria");
  8. names.Add("Bill");
  9. names.Remove("Ana");
  10. foreach (var name in names)
  11. {
  12. Console.WriteLine($"Hello {name.ToUpper()}!");
  13. }
  14. Console.WriteLine($"The list has {names.Count} people in it");
  15. /* Hello <NAME>!
  16. Hello ANA!
  17. Hello FELIPE!
  18. Hello <NAME>!
  19. Hello FELIPE!
  20. Hello MARIA!
  21. Hello BILL!
  22. The list has 4 people in it */

搜索和排序 .IndexOf .Sort()

  1. // 搜索和排序
  2. var index = names.IndexOf("Felipe");
  3. if (index != -1)
  4. Console.WriteLine($"The name {names[index]} is at index {index}");
  5. var notFound = names.IndexOf("Not Found");
  6. Console.WriteLine($"When an item is not found, IndexOf returns {notFound}");
  7. names.Sort();
  8. foreach (var name in names)
  9. {
  10. Console.WriteLine($"Hello {name.ToUpper()}!");
  11. }