`

c# - Collection initializing divergence in array and collection such as List

    博客分类:
  • C#
c# 
阅读更多

You may have seen the comment that I have put in the code in the post : c# - Sample of Lambda expression on List<T> and Events.. And also, I have keep some post on the discussion of Collection Initializer.

 

There is subtlety in terms of how you write 

 

 

var names = new List<string>{
    "Holy",
    "Jon",
    "Tom"

};
 

and 

 

 

var names = new string [] {
    "Holy",
    "Jon",
    "Tom"
};

 

The former (the List<string> case) will actually calls the List.Add(...) method, while the later, construct some temporary array instance, and assign the reference to the names variable. 

 

so that explain why you can do 

 

 

var form = new Form { AutoSize = true, Controls = { button } };
    

 but not to 

 

 

var form = new Form { AutoSize = true, Controls = new []{ button } };
 

 

分享到:
评论

相关推荐

    c-programming-for-scientists-and-engineers-manufacturing-engineering-series

    This section covers the precision differences between `float` and `double`, as well as best practices for handling floating-point numbers, such as rounding and truncation. #### 1.5 The Pointer ...

    Google C++ Style Guide_英文版.pdf

    Static member functions can be useful in certain contexts, such as factory methods. #### Classes - **Doing Work in Constructors:** Constructors should perform only initialization and not complex ...

    话费充值系统。。。。。

    2020-11-27 11:26:11|localhost-startStop-1|INFO |com.mchange.v2.c3p0.C3P0Registry,204,banner|-&gt; Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10] 2020-11-27 11:26:11|...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    - **Simple Types:** Explains primitive data types such as integers, floats, and characters. - **Initialization:** Describes different methods of initializing variables. - **The Traditional Conversions...

    fewpjs-oo-initializing-instances-online-web-sp-000

    初始化实例 学习目标 使用class关键字在JavaScript中创建一个类 提供实例JavaScript类的构造函数实例化数据 介绍 在本实验中,我们将练习使用JavaScript的class关键字创建面向对象的类和实例。 我们还将使用...

    SCJP6 Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065) 英文原版

    - **Array Assignments**: Creating and initializing arrays, including multi-dimensional arrays. #### 4. Operators Operators are essential for performing operations on operands. This chapter explores:...

    The gdk-pixbuf Library

    gdk-pixbuf Xlib initialization - Initializing the gdk-pixbuf Xlib library. Xlib Rendering - Rendering a pixbuf to an X drawable. X Drawables to Pixbufs - Getting parts of an X drawable's image data ...

    C Programming

    - **Getting Values into the Program**: Discussion on how to read values from external sources, such as keyboard input, and store them in variables. #### Writing a Program This section provides ...

    Essential C

    The guide covers a wide range of topics, including basic data types, operators, control structures, complex data types, functions, and advanced concepts such as pointers and heap memory management. ...

    HTML5 GAMES Creating Fun with HTML5 CSS3 and WebGL 英文PDF

    - **Creating the Game Board Module**: Explained as initializing the game state and filling the initial board with game pieces. - **Implementing the Rules**: Detailed as validating swaps, detecting ...

    TMS Pack for FireMonkey2.3.0.1

    Fixed : Issue with list read access in TTMSFMXNavBar Fixed : incorrect Gutter behavior when gutter is visible false or width = 0 in TTMSFMXMemo Fixed : Issue with XE2 FMI package memo registration ...

    ILI9325AN_V0.14.pdf

    - **Application**: This chip is suitable for various applications that require high-quality color display outputs, such as portable devices, industrial controls, and other electronic gadgets. ...

    基于51单片机控制的点阵LED电子显示屏设计说明.doc

    The use of C51 microcontrollers in this design takes advantage of their built-in features, such as the programmable I/O ports, timers, and interrupts, which are essential for the operation of the LED ...

    Bare-metal Boot Code for ARMv8-A Processors

    Hardware verification engineers often run bare-metal tests to verify core-related function in a System on Chip (SoC). However, it can be challenging to write boot code for a bare-metal system, without...

    2009 达内Unix学习笔记

    ls -l (list)列表显示文件(默认按文件名排序), 显示文件的权限、硬链接数(即包含文件数,普通文件是1,目录1+)、用户、组名、大小、修改日期、文件名。 ls -t (time)按修改时间排序,显示目录和文件。 ls -lt 是...

Global site tag (gtag.js) - Google Analytics