提交 04292013 编写于 作者: Admini$trat0r's avatar Admini$trat0r

添加1..NET初阶\3.C#特性\1.C#3.0特性(隐式类型本地变量,分部方法,对象和集合初始值设定项)

上级 2b4f2f9b
{
"type": "code_options",
"author": "Gao996",
"source": "ImplicitType.md",
"exercise_id": "41982620620243ef956a14e95c584d2d",
"notebook_enable": false
}
\ No newline at end of file
# 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
{
"type": "code_options",
"author": "Gao996",
"source": "ObjectAndCollectionInitializers.md",
"exercise_id": "7bb7e2531e7a426cbd731720fa33d0ba",
"notebook_enable": false
}
\ No newline at end of file
# 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<int> digits = new List<int> { 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<int, string>()
{
7 = "seven",
9 = "nine",
13 = "thirteen"
};
```
## 选项
### A
```csharp
var numbers = new Dictionary<int, string>()
{
[1] = "one",
[9] = "nine",
[13] = "thirteen"
};
```
### B
```csharp
var numbers = new Dictionary<int, string>()
{
{19, "nineteen" },
{23, "twenty-three" },
{42, "forty-two" }
};
```
### C
```csharp
var numbers = new Dictionary<int, string>()
{
};
```
\ No newline at end of file
{
"type": "code_options",
"author": "Gao996",
"source": "PartialMethod",
"exercise_id": "4a743fa6bf5e49cf8fff28ed0f9e6bef",
"notebook_enable": false
}
\ No newline at end of file
# C# 3.0 特性 分部方法
分部方法在分部类型的一部分中定义了签名,并在该类型的另一部分中定义了实现。比如WPF和WinForm的窗体就是用partial关键字定义的分部类,一部分代码可以实现控件的初始化,一部分可以去实现具体方法等。
```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
......@@ -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<int> digits = new List<int> { 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册