详解C#中使用对象或集合的初始值设定项初始化的操作
作者:goldensun 发布时间:2021-12-25 20:04:52
标签:C#,初始化
使用对象初始值设定项初始化对象
可以使用对象初始值设定项以声明方式初始化类型对象,而无需显式调用类型的构造函数。
下面的示例演示如何将对象初始值设定项用于命名对象。编译器通过先访问默认实例构造函数然后处理成员初始化处理对象初始值设定项。因此,如果默认构造函数在类中声明为 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> 中的键/值对的初始值。 最后,字典的整个集合初始值括在一对大括号内。
0
投稿
猜你喜欢
- 前言:mybatisplus 可以说是对mybatis更好的拓展,一些简单的增删改查的操作已经被作者实现,我们只需引用即可。1.数据库建表这
- 1.map遍历快速实现边距,文字自适应改变大小Container( // padding: EdgeI
- 背景描述通常如果需要一次更新多条数据有两个方式:(1)在业务代码中循环遍历逐条更新。(2)一次性更新所有数据(更准确的说是一条sql语句来更
- 本文讲述了在Java中如何创建和结束线程的最基本方法,只针对于Java初学者。一些高级知识如线程同步、调度、线程池等内容将会在后续章节中逐步
- 背景公司开发框架增加了web系统license授权证书校验模块,实行一台机器一个授权证书,初步方案是增加 * 针对全局请求进行拦截校验,评估
- 下面给大家介绍几种比较常见的解决办法,具体内容如下:1.有时候eclipse不自动编译,把project clean一下,让R.java重新
- 介绍该系统有三个角色,分别是:普通用户、房屋中介、管理员。普通用户的功能:浏览房屋信息、预约看房、和中介聊天、申请成为中介等等。房屋中介的功
- Lambda表达式无法抛出异常1.Demo 例子错误提示 - Unhandled exception: java.io.IOExceptio
- Spring是一个非常流行的Java Web开发框架,它提供了强大的依赖注入、面向切面编程、声明式事务管理等功能,为开发者提供了高效、快速地
- 插件安装方式:新版本IDE安装方式略有不同,不一一赘述 1、Background Image Plus
- springboot就是简化Spring应用中的初始化配置,快速创建项目而生的。创建springboot项目代开idea,点击File—&g
- XmlTextReader 提供对 XML 数据流的只进只读访问。当前节点指读取器定位到的节点。使用任何读方法推进读取器并且属性反映当前节点
- Mapper:@Mapper@OracleRepositorypublic interface OracleRadiusMapper{@In
- 前言本文主要跟大家介绍了关于Java用gson解析Json的相关内容,分享出来供大家参考学习,需要的朋友们下面来一起看看吧。json数据{&
- 在分布式系统中,配置文件散落在每个项目中,难于集中管理,抑或修改了配置需要重启才能生效。下面我们使用 Spring Cloud Config
- 注意是maven的webapp:选择maven下一步下一步。maven下载过慢在setting中加入镜像。 我也有疑问这是什么鬼格式,但是证
- 以前使用HttpServletResponse可以通过输出流的方式来向前台输出图片。现在大部分都是使用springboot,在使用sprin
- 本文实例讲述了Java实现的Base64加密算法。分享给大家供大家参考,具体如下:一 算法实现1、JDK2、Commonc Codec3、B
- 【题目】 汉诺塔问题比较经典,这里修改一下游戏规则:现在限制不能从最左侧的塔直接移动到最右侧,也不能从最右侧直接移动到最左侧,而是必须经过中
- @RequestMapping注解注意点类上加没加@RequestMappin注解区别1.如果类上加了 @RequestMappin注解,那