diff --git "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/3.C#\345\255\227\347\254\246\344\270\262\346\240\274\345\274\217\350\256\276\347\275\256/StringFormatting.md" "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/3.C#\345\255\227\347\254\246\344\270\262\346\240\274\345\274\217\350\256\276\347\275\256/StringFormatting.md" index 65bf2a98ec216521b1205eb95f3a36a9e00da486..c27e2bc229d10c3f294b9f8e120bfae4bed23b15 100644 --- "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/3.C#\345\255\227\347\254\246\344\270\262\346\240\274\345\274\217\350\256\276\347\275\256/StringFormatting.md" +++ "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/3.C#\345\255\227\347\254\246\344\270\262\346\240\274\345\274\217\350\256\276\347\275\256/StringFormatting.md" @@ -1,4 +1,4 @@ -# 在C#中执行基本字符串格式设置 +# 在C#中执行基本字符串格式设置 System.String是一个类,专门用于存储字符串,允许对字符串进行许多操作。由于这种数据类 型非常重要,C#提供了它自己的关键字和相关的语法,以便于使用这个类来轻松地处理字符串。 @@ -8,8 +8,8 @@ String 源码网址: https://referencesource.microsoft.com/#mscorlib/system/stri ```csharp string message1 = "Hello"; // return "Hello" -message1 += ", CodeChina"; // return "Hello, CodeChina" -string message2 = message1 + "!"; //return "Hello, CodeChina!" +message1 += ", CSDN"; // return "Hello, CSDN" +string message2 = message1 + "!"; //return "Hello, CSDN!" ``` C#还允许使用类似于索引器的语法来提取指定的字符: @@ -47,13 +47,13 @@ string str1 = $"6{count}6"; // return "666" string str2 = @"6\r6\n6"; //return "6\r6\n6" ``` -以下程序中,不能使str3等于`"Hello, CodeChina!"`的是: +以下程序中,不能使str3等于`"Hello, CSDN!"`的是: ## 答案 ```csharp var str1 = "Hello, "; -var str2 = "CodeChina!"; +var str2 = "CSDN!"; var str3 = "str1" + "str2"; ``` @@ -63,7 +63,7 @@ var str3 = "str1" + "str2"; ```csharp var str1 = "Hello, "; -var str2 = "CodeChina!"; +var str2 = "CSDN!"; var str3 = str1 + str2; ``` @@ -71,7 +71,7 @@ var str3 = str1 + str2; ```csharp var str1 = "Hello"; -var str2 = "CodeChina"; +var str2 = "CSDN"; var str3 = string.Format("{0}, {1]!", str1, str2); ``` @@ -79,7 +79,7 @@ var str3 = string.Format("{0}, {1]!", str1, str2); ```csharp var str1 = "Hello, "; -var str2 = "CodeChina!"; +var str2 = "CSDN!"; var str3 = string.Concat(str1, str2); ``` diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ImplicitType.json" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ImplicitType.json" new file mode 100644 index 0000000000000000000000000000000000000000..f1145c3e29c9f9f258f54b38b42fd4580f3fc6f1 --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ImplicitType.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "Gao996", + "source": "ImplicitType.md", + "exercise_id": "41982620620243ef956a14e95c584d2d", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ImplicitType.md" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ImplicitType.md" new file mode 100644 index 0000000000000000000000000000000000000000..4cbf609d432b4e310f9ab2b9d5c196637bb09a39 --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ImplicitType.md" @@ -0,0 +1,31 @@ +# C# 3.0 ʽͱر + +C# 3.0 ʼڷΧıԾʽ͡varʽͱرΪǿͣûѾͣһ + +```var i = Hello CSDN;```룬ѡiǣ + +## + +```csharp +string +``` + +## ѡ + +### A + +```csharp +int +``` + +### B + +```csharp +char +``` + +### C + +```csharp +double +``` \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ObjectAndCollectionInitializers.json" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ObjectAndCollectionInitializers.json" new file mode 100644 index 0000000000000000000000000000000000000000..404a1cb196b871095767d5e9096693cefdbf1d35 --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ObjectAndCollectionInitializers.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "Gao996", + "source": "ObjectAndCollectionInitializers.md", + "exercise_id": "7bb7e2531e7a426cbd731720fa33d0ba", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ObjectAndCollectionInitializers.md" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ObjectAndCollectionInitializers.md" new file mode 100644 index 0000000000000000000000000000000000000000..f6d6ac4c5e31aaecfd00f46f080590b0dcd36b0f --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ObjectAndCollectionInitializers.md" @@ -0,0 +1,113 @@ +# C# 3.0 特性 对象和集合初始值设定项 + +使用对象初始值设定项,你可以在创建对象时向对象的任何可访问字段或属性分配值,而无需调用后跟赋值语句行的构造函数。 比如下面的Cat类,以往创建对象可能 +```csharp +Cat cat = new Cat(); +cat.Name = "Tom"; +cat.Age = 10; +``` +当然可以直接为对象设置初始值,这样可以简化对象的构造,如下例子: + +```csharp +public class Cat +{ + // 自动实现的属性 + public int Age { get; set; } + public string Name { get; set; } + + public Cat() + { + } + + public Cat(string name) + { + this.Name = name; + } +} +``` +```csharp +Cat cat = new Cat { Age = 10, Name = "Fluffy" }; +Cat sameCat = new Cat("Fluffy"){ Age = 10 }; +``` +上面这段代码示例了使用具有命名类型Cat的对象初始值设定项以及如何调用无参数构造函数。当然也可以为匿名类型设置初始值: +```csharp +var testA = new { name = "Gao", age = 996 }; +``` +下面是一个List集合的初始化例子: +```csharp +List digits = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; +``` +除了分配字段和属性外,对象初始值设定项还可以设置索引器,如下例子: +```csharp +public class MatrixTest +{ + private readonly double[,] storage = new double[3, 3]; + + public double this[int row, int column] + { + get { return storage[row, column]; } + set { storage[row, column] = value; } + } +} +``` +```csharp +var identity = new MatrixTest() +{ + [0, 0] = 1.0, + [0, 1] = 0.0, + [0, 2] = 0.0, + + [1, 0] = 0.0, + [1, 1] = 1.0, + [1, 2] = 0.0, + + [2, 0] = 0.0, + [2, 1] = 0.0, + [2, 2] = 1.0 +}; +``` + +在了解了对象和集合初始值的设定之后,实现对Dictionary初始值设定,以下选项错误的是: + +## 答案 + +```csharp +var numbers = new Dictionary() +{ + 7 = "seven", + 9 = "nine", + 13 = "thirteen" +}; +``` + +## 选项 + +### A + +```csharp +var numbers = new Dictionary() +{ + [1] = "one", + [9] = "nine", + [13] = "thirteen" +}; +``` + +### B + +```csharp +var numbers = new Dictionary() +{ + {19, "nineteen" }, + {23, "twenty-three" }, + {42, "forty-two" } +}; +``` + +### C + +```csharp +var numbers = new Dictionary() +{ +}; +``` \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/PartialMethod.json" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/PartialMethod.json" new file mode 100644 index 0000000000000000000000000000000000000000..54346b85475ccbb4a065506ff4543f7524ef89da --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/PartialMethod.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "Gao996", + "source": "PartialMethod", + "exercise_id": "4a743fa6bf5e49cf8fff28ed0f9e6bef", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/PartialMethod.md" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/PartialMethod.md" new file mode 100644 index 0000000000000000000000000000000000000000..b727947b38cb74b1f1f7d85524efab4e56d046fc --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/PartialMethod.md" @@ -0,0 +1,52 @@ +# C# 3.0 ֲ + +ֲڷֲ͵һжǩڸ͵һжʵ֡WPFWinFormĴpartialؼֶķֲ࣬һִʵֿؼijʼһֿȥʵ־巽ȡ + +```csharp +partial class PartialClassTest +{ + readonly string str = "Hello CSDN!"; + + partial void PartialMethodTest(); + + public void Display() + { + PartialMethodTest(); + } +} + +partial class PartialClassTest +{ + partial void PartialMethodTest() + { + Console.WriteLine(str); + } +} +``` +δʾ˷ֲͷִ```Display()```ѡпԴӡַǣ + +## + +```csharp +Hello CSDN! +``` + +## ѡ + +### A + +```csharp +str +``` + +### B + +```csharp +null +``` + +### C + +```csharp +׳쳣 +``` \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/sample/Program.cs" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/sample/Program.cs" index f4d1c02c9a187e9f1ecd45858cafcc693b637535..e343d40080415f2f25d5fa6e6461cd11bb81cc26 100644 --- "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/sample/Program.cs" +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/sample/Program.cs" @@ -63,5 +63,76 @@ public static class Extensions } // 隐式类型本地变量 +var i = “Hello CSDN!”; + // 分部方法 -// 对象和集合初始值设定项 \ No newline at end of file +partial class PartialClassTest +{ + readonly string str = "Hello CSDN!"; + + partial void PartialMethodTest(); + + public void Display() + { + PartialMethodTest(); + } +} + +partial class PartialClassTest +{ + partial void PartialMethodTest() + { + Console.WriteLine(str); + } +} + + +// 对象和集合初始值设定项 +public class Cat +{ + // 自动实现的属性 + public int Age { get; set; } + public string Name { get; set; } + + public Cat() + { + } + + public Cat(string name) + { + this.Name = name; + } +} + +Cat cat = new Cat { Age = 10, Name = "Fluffy" }; +Cat sameCat = new Cat("Fluffy"){ Age = 10 }; + +var testA = new { name = "Gao", age = 996 }; + +List digits = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + +public class MatrixTest +{ + private readonly double[,] storage = new double[3, 3]; + + public double this[int row, int column] + { + get { return storage[row, column]; } + set { storage[row, column] = value; } + } +} + +var identity = new MatrixTest() +{ + [0, 0] = 1.0, + [0, 1] = 0.0, + [0, 2] = 0.0, + + [1, 0] = 0.0, + [1, 1] = 1.0, + [1, 2] = 0.0, + + [2, 0] = 0.0, + [2, 1] = 0.0, + [2, 2] = 1.0 +}; \ No newline at end of file