详解C#中使用对象或集合的初始值设定项初始化的操作
作者:goldensun 时间:2021-12-25 20:04:52
使用对象初始值设定项初始化对象
可以使用对象初始值设定项以声明方式初始化类型对象,而无需显式调用类型的构造函数。
下面的示例演示如何将对象初始值设定项用于命名对象。编译器通过先访问默认实例构造函数然后处理成员初始化处理对象初始值设定项。因此,如果默认构造函数在类中声明为 private,那么需要公共访问权的对象初始值设定项将失败。
下面的示例演示如何使用对象初始值设定项初始化新的 StudentName 类型。
public class Program
{
public static void Main()
{
// Declare a StudentName by using the constructor that has two parameters.
StudentName student1 = new StudentName("Craig", "Playstead");
// Make the same declaration by using an object initializer and sending
// arguments for the first and last names. The default constructor is
// invoked in processing this declaration, not the constructor that has
// two parameters.
StudentName student2 = new StudentName
{
FirstName = "Craig",
LastName = "Playstead",
};
// Declare a StudentName by using an object initializer and sending
// an argument for only the ID property. No corresponding constructor is
// necessary. Only the default constructor is used to process object
// initializers.
StudentName student3 = new StudentName
{
ID = 183
};
// Declare a StudentName by using an object initializer and sending
// arguments for all three properties. No corresponding constructor is
// defined in the class.
StudentName student4 = new StudentName
{
FirstName = "Craig",
LastName = "Playstead",
ID = 116
};
System.Console.WriteLine(student1.ToString());
System.Console.WriteLine(student2.ToString());
System.Console.WriteLine(student3.ToString());
System.Console.WriteLine(student4.ToString());
}
}
这段的输出是:
Craig 0
Craig 0
183
Craig 116
public class StudentName
{
// The default constructor has no parameters. The default constructor
// is invoked in the processing of object initializers.
// You can test this by changing the access modifier from public to
// private. The declarations in Main that use object initializers will
// fail.
public StudentName() { }
// The following constructor has parameters for two of the three
// properties.
public StudentName(string first, string last)
{
FirstName = first;
LastName = last;
}
// Properties.
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public override string ToString()
{
return FirstName + " " + ID;
}
}
下面的示例演示如何使用集合初始值设定项初始化一个 StudentName 类型集合。请注意,集合初始值设定项是一系列由逗号分隔的对象初始值设定项。
List<StudentName> students = new List<StudentName>()
{
new StudentName {FirstName="Craig", LastName="Playstead", ID=116},
new StudentName {FirstName="Shu", LastName="Ito", ID=112},
new StudentName {FirstName="Gretchen", LastName="Rivas", ID=113},
new StudentName {FirstName="Rajesh", LastName="Rotti", ID=114}
};
使用集合初始值设定项初始化字典
Dictionary<TKey, TValue> 包含键/值对集合。 它的 Add 方法采用两个参数,一个用于键,另一个用于值。 若要初始化 Dictionary<TKey, TValue> 或其 Add 方法采用多个参数的任何集合,请将每组参数括在大括号中,如下面的示例所示。
示例
在下面的代码示例中,使用 StudentName 类型的实例初始化一个 Dictionary<TKey, TValue>。
class StudentName
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
}
class CollInit
{
Dictionary<int, StudentName> students = new Dictionary<int, StudentName>()
{
{ 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}},
{ 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}},
{ 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}}
};
}
请注意集合的每个元素中的两对大括号。 最内层的大括号括起了 StudentName 的对象初始值,而最外层的大括号括起了将要添加到 studentsDictionary<TKey, TValue> 中的键/值对的初始值。 最后,字典的整个集合初始值括在一对大括号内。
标签:C#,初始化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
简单了解Java多态向上转型相关原理
2023-10-11 16:11:01
线程池中execute与submit的区别说明
2023-03-18 23:09:04
![](https://img.aspxhome.com/file/2023/6/64786_0s.png)
浅谈利用Spring的AbstractRoutingDataSource解决多数据源的问题
2021-09-07 07:20:59
c# Linq查询详解
2023-05-23 20:43:50
![](https://img.aspxhome.com/file/2023/0/68620_0s.png)
Java多线程之同步锁-lock详解
2023-12-16 14:40:08
![](https://img.aspxhome.com/file/2023/0/62230_0s.png)
详解JAVA流程控制语句
2023-11-05 02:27:09
![](https://img.aspxhome.com/file/2023/1/58711_0s.png)
JAVA格式化时间日期的简单实例
2022-10-06 09:14:58
Mybatis Plus中的流式查询案例
2023-08-18 16:35:13
Android编程实现WebView添加进度条的方法
2023-07-06 03:16:46
java中Hashmap的get方法使用
2023-10-29 13:10:05
![](https://img.aspxhome.com/file/2023/2/58602_0s.png)
Netty分布式pipeline管道异常传播事件源码解析
2021-08-15 16:12:02
Java经典面试题最全汇总208道(四)
2023-11-08 23:59:26
![](https://img.aspxhome.com/file/2023/5/59065_0s.png)
SpringBoot如何使用ApplicationContext获取bean对象
2023-06-28 20:36:55
![](https://img.aspxhome.com/file/2023/1/67101_0s.jpg)
android图片处理 让图片变成圆形
2023-10-03 00:47:30
![](https://img.aspxhome.com/file/2023/6/83536_0s.jpg)
谈谈Java类型中ParameterizedType,GenericArrayType,TypeVariabl,WildcardType
2022-10-18 17:43:07
SpringBoot2整合activiti6环境搭建过程解析
2023-11-09 02:27:03
![](https://img.aspxhome.com/file/2023/9/59379_0s.png)
C#实现最完整的文件和目录操作类实例
2022-05-19 09:38:51
Java内存模型之happens-before概念详解
2023-11-23 03:11:50
![](https://img.aspxhome.com/file/2023/1/59971_0s.png)
IDEA+Maven搭建Spring环境的详细教程
2023-11-25 07:50:34
![](https://img.aspxhome.com/file/2023/0/59880_0s.png)
关于MD5算法原理与常用实现方式
2023-03-18 11:09:04
![](https://img.aspxhome.com/file/2023/8/62828_0s.png)