安装 Visual Studio 2022
字符类型
// 格式化输出
string firstFriend = "Maria";
string secondFriend = "Sage";
aFriend = "Maira";
Console.WriteLine("Hello " + aFriend);
Console.WriteLine($"My friends are {firstFriend} and {secondFriend}");
// 清理空格 .TrimStart()
.TrimEnd()
.Trim()
// 清理空格 .TrimStart() .TrimEnd() .Trim()
string greeting = " Hello World! ";
Console.WriteLine($"[{greeting}]");
// [ Hello World! ]
string trimmedGreeting = greeting.TrimStart();
Console.WriteLine($"[{trimmedGreeting}]");
// [Hello World! ] .TrimStart()
trimmedGreeting = greeting.TrimEnd();
Console.WriteLine($"[{trimmedGreeting}]");
// [ Hello World!] .TrimEnd()
trimmedGreeting = greeting.Trim();
Console.WriteLine($"[{trimmedGreeting}]");
// [Hello World!] .Trim()
// 字符替换.Replace()
// 字符替换 .Replace()
string sayHello = "Hello World!";
Console.WriteLine(sayHello);
sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);
// 大小写转换 .ToUpper()
.ToLower()
// 大小写转换 .ToUpper() .ToLower()
Console.WriteLine(sayHello.ToUpper());
Console.WriteLine(sayHello.ToLower());
// 字符串匹配 .Contains()
.StartsWith()
.EndsWith()
// 字符串匹配 .Contains() .StartsWith() .EndsWith()
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye")); //True
Console.WriteLine(songLyrics.Contains("greetings")); //False
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.StartsWith("You")); //True
Console.WriteLine(songLyrics.StartsWith("goodbye")); //False
Console.WriteLine(songLyrics.EndsWith("hello")); //True
Console.WriteLine(songLyrics.EndsWith("goodbye")); //False
// 逐字字符串文本
Console.WriteLine(@" c:\source\repos
(this is where your code goes)");
// c:\source\repos
// (this is where your code goes)
// Unicode 转义字符
// Kon'nichiwa World
Console.WriteLine("\u3053\u3093\u306B\u3061\u306F World!");
//こんにちは World!
// 混合数字计算的 格式化输出
string firstName = "Bob";
int widgetsSold = 7;
Console.WriteLine(firstName + " sold " + (widgetsSold + 7) + " widgets.");
数字类型 (多了一个decimal类型)
//十进制类型的范围较小,但精度高于双倍。
//The decimal type has a smaller range
//but greater precision than double.
double a = 1.0;
double b = 3.0;
Console.WriteLine(a / b);
//0.333333333333333
decimal c = 1.0M;
decimal d = 3.0M;
Console.WriteLine(c / d);
//0.3333333333333333333333333333
注:数字上的M后缀是您表示常量应该使用十进制类型的方式。 否则,编译器假定双重类型。 字母M被选为double
和decimal
关键字之间最有视觉上的字母。
列表类型
var names = new List<string> { "<name>", "Ana", "Felipe" };
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}
Console.WriteLine();
names.Add("Maria");
names.Add("Bill");
names.Remove("Ana");
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}
Console.WriteLine($"The list has {names.Count} people in it");
/* Hello <NAME>!
Hello ANA!
Hello FELIPE!
Hello <NAME>!
Hello FELIPE!
Hello MARIA!
Hello BILL!
The list has 4 people in it */
搜索和排序 .IndexOf
.Sort()
// 搜索和排序
var index = names.IndexOf("Felipe");
if (index != -1)
Console.WriteLine($"The name {names[index]} is at index {index}");
var notFound = names.IndexOf("Not Found");
Console.WriteLine($"When an item is not found, IndexOf returns {notFound}");
names.Sort();
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}