转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件使用 >> 系统软件 >> 正文
14.5.10.2 Array creation expressions         

14.5.10.2 Array creation expressions

作者:闵涛 文章来源:闵涛的学习笔记 点击数:865 更新时间:2009/4/25 0:44:46
An array-creation-expression is used to create a new instance of an
array-type.
array-creation-expression:
new non-array-type [ expression-list ] rank-specifiersopt
array-initializeropt
new array-type array-initializer
An array creation expression of the first form allocates an array instance
of the type that results from
deleting each of the individual expressions from the expression list.
[Example: For example, the array
creation expression new int[10,20] produces an array instance of type
int[,], and the array creation
expression new int[10][,] produces an array of type int[][,]. end example]
Each expression in the
expression list must be of type int, uint, long, or ulong, or of a type
that can be implicitly converted to
one or more of these types. The value of each expression determines the
length of the corresponding
dimension in the newly allocated array instance. Since the length of an
array dimension must be
C# LANGUAGE SPECIFICATION
148
nonnegative, it is a compile-time error to have a constant expression with
a negative value, in the expression
list.
Except in an unsafe context (?5.1), the layout of arrays is unspecified.
If an array creation expression of the first form includes an array
initializer, each expression in the
expression list must be a constant and the rank and dimension lengths
specified by the expression list must
match those of the array initializer.
In an array creation expression of the second form, the rank of the
specified array type must match that of
the array initializer. The individual dimension lengths are inferred from
the number of elements in each of
the corresponding nesting levels of the array initializer. Thus, the
expression
new int[,] {{0, 1}, {2, 3}, {4, 5}}
exactly corresponds to
new int[3, 2] {{0, 1}, {2, 3}, {4, 5}}
Array initializers are described further in ?9.6.
The result of evaluating an array creation expression is classified as a
value, namely a reference to the newly
allocated array instance. The run-time processing of an array creation
expression consists of the following
steps:
?The dimension length expressions of the expression-list are evaluated in
order, from left to right.
Following evaluation of each expression, an implicit conversion (?3.1) to
one of the following types is
performed: int, uint, long, ulong. The first type in this list for which an
implicit conversion exists is
chosen. If evaluation of an expression or the subsequent implicit
conversion causes an exception, then
no further expressions are evaluated and no further steps are executed.
?The computed values for the dimension lengths are validated, as follows:
If one or more of the values
are less than zero, a System.OverflowException is thrown and no further
steps are executed.
?An array instance with the given dimension lengths is allocated. If there
is not enough memory available
to allocate the new instance, a System.OutOfMemoryException is thrown and
no further steps are
executed.
?All elements of the new array instance are initialized to their default
values (?2.2).
?If the array creation expression contains an array initializer, then each
expression in the array initializer
is evaluated and assigned to its corresponding array element. The
evaluations and assignments are
performed in the order the expressions are written in the array
initializer?in other words, elements are
initialized in increasing index order, with the rightmost dimension
increasing first. If evaluation of a
given expression or the subsequent assignment to the corresponding array
element causes an exception,
then no further elements are initialized (and the remaining elements will
thus have their default values).
An array creation expression permits instantiation of an array with
elements of an array type, but the
elements of such an array must be manually initialized. [Example: For
example, the statement
int[][] a = new int[100][];
creates a single-dimensional array with 100 elements of type int[]. The
initial value of each element is
null. end example] It is not possible for the same array creation
expression to also instantiate the subarrays,
and the statement
int[][] a = new int[100][5]; // Error
results in a compile-time error. Instantiation of the sub-arrays must
instead be performed manually, as in
int[][] a = new int[100][];
for (int i = 0; i < 100; i++) a[i] = new int[5];
When an array of arrays has a ?rectangular? shape, that is when the
sub-arrays are all of the same length, it is
more efficient to use a multi-dimensional array. In the example above,
instantiation of the array of arrays
creates 101 objects?one outer array and 100 sub-arrays. In contrast,
Chapter 14 Expressions
149
int[,] = new int[100, 5];
creates only a single object, a two-dimensional array, and accomplishes the
allocation in a single statement.


[系统软件]14.5.10.3 Delegate creation expressions  [系统软件]14.5.10.1 Object creation expressions
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · 办公软件  · 系统软件
    · 常用软件  · 聊天工具
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台