public class Cat { // Auto-implemented properties. public int Age { get; set; } public string Name { get; set; } public Cat() { } public Cat(string name) { this.Name = name; } }

var moreNumbers = new Dictionary<int, string>
{
{19, "nineteen" },
{23, "twenty-three" },
{42, "forty-two" }
};

Starting with C# 6, object initializers can set indexers, in addition to assigning fields and properties. Consider this basic Matrix class:

public class Matrix {

private double[,] storage = new double[3, 3];

public double this[int row, int column] { // The embedded array will throw out of range exceptions as appropriate. get { return storage[row, column]; } set { storage[row, column] = value; } } }

var identity = new Matrix { [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, };

这个有意思:就是用lambda 表达式来初始化类

public static void Main()
{
FormattedAddresses addresses = new FormattedAddresses()
{
{"John", "Doe", "123 Street", "Topeka", "KS", "00000" },
{"Jane", "Smith", "456 Street", "Topeka", "KS", "00000" }
};

Console.WriteLine("Address Entries:");

foreach (string addressEntry in addresses)
{
Console.WriteLine("\r\n" + addressEntry);
}
}

/*
* Prints:

Address Entries:

John Doe
123 Street
Topeka, KS 00000

Jane Smith
456 Street
Topeka, KS 00000
*/
}

Object and Collection Initializers (C# Programming Guide) 类初始化的更多相关文章

  1. Collection View Programming Guide for iOS---(七)---Custom Layouts: A Worked Example

    Custom Layouts: A Worked Example Creating a custom collection view layout is simple with straightfor ...

  2. Collection View Programming Guide for iOS---(三)---Designing Your Data Source and Delegate

      Designing Your Data Source and Delegate 设计你的数据源和委托 Every collection view must have a data source o ...

  3. Collection View Programming Guide for iOS---(一)----About iOS Collection Views

    Next About iOS Collection Views 关于iOS Collection Views A collection view is a way to present an orde ...

  4. Collection View Programming Guide for iOS---(六)---Creating Custom Layouts

    Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...

  5. Collection View Programming Guide for iOS---(二)----Collection View Basics

      Collection View Basics Collection View 基础 To present its content onscreen, a collection view coope ...

  6. View Controller Programming Guide for iOS---(五)---Resource Management in View Controllers

    Resource Management in View Controllers View controllers are an essential part of managing your app’ ...

  7. View Controller Programming Guide for iOS---(二)---View Controller Basics

    View Controller Basics Apps running on iOS–based devices have a limited amount of screen space for d ...

  8. Table View Programming Guide for iOS---(四)---Navigating a Data Hierarchy with Table Views

    Navigating a Data Hierarchy with Table Views 导航数据表视图层次 A common use of table views—and one to which ...

  9. View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views

    Views Because view objects are the main way your application interacts with the user, they have many ...

随机推荐

  1. Retina 屏幕与二倍图

    分辨率 屏幕分辨率:指屏幕可显示的像素的个数 图像分辨率:位图图像包含的像素的个数 对于 Retina 屏它的分辨率是传统屏的两倍,而屏幕大小没有变化,所以它需要的图片的分辨率应该是传统屏幕的两倍(甚 ...

  2. Chromely

    Chromely Chromely is a lightweight alternative to Electron.NET, Electron for .NET/.NET Core develope ...

  3. Krypton Suite of .NET WinForms Controls

    The Krypton Suite of .NET WinForms controls are now freely available for use in personal or commeric ...

  4. 浅谈Java反射机制 之 获取类的字节码文件 Class.forName("全路径名") 、getClass()、class

    另一个篇:获取 类 的 方法 和 属性(包括构造函数) 先贴上Java反射机制的概念: AVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它 ...

  5. 简单DP入门(一) 数字三角形

    数字三角形

  6. 信息收集【采集点OWASP CHINA】网址http://www.owasp.org.cn/

    以下部分源于 安全家 http://www.anquanjia.net.cn/newsinfo/729480.html 资源虽多,优质却少.不要被信息海迷惑的心智,新人要想入门,除了优质的系统教学资源 ...

  7. 001--PowerDesigner连接MySQL

    PowerDesigner连接MySQL(一) 博客地址:https://blog.csdn.net/codemonkey_king/article/details/53263597 https:// ...

  8. string类find_first_not_of ()方法

    string类find_first_not_of ()方法 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xfqxj.blog. ...

  9. [19/05/13-星期一] HTML_head标签 和 body标签_文本标签

    一.概念 概念:超文本标记语言 作用:需要将Java在后台根据用户的请求处理结果在浏览器显示给用户.数据已经过来了,但是显示可能比较凌乱,所以html应用而生,就像写作文要加标点看着舒服. 在浏览器中 ...

  10. Codeforces 609E (Kruskal求最小生成树+树上倍增求LCA)

    题面 传送门 题目大意: 给定一个无向连通带权图G,对于每条边(u,v,w)" role="presentation" style="position: rel ...