带或不带括号的新列表 [英] new List with or without parentheses

查看:136
本文介绍了带或不带括号的新列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

我们可以通过两种类似的方式来创建已知字节的列表:

Hi experts,

we can create a list of known bytes in two similar ways:

List<byte> withParentheses = new List<byte>() {1,2,3,4,5};
List<byte> withoutParentheses = new List<byte> {6,7,8,9,10};

两者似乎都有效.

两者有什么区别?

Both seem to work.

What is the difference between the two?
Is one of them preferrable over the other?

推荐答案

就您的示例而言,什么都不是.

在第一种情况下,您实际上可以在第一条语句中定义数组计数,但稍后再为其添加值.
例如
列出< byte> withParentheses =新列表< byte> [4];
withParentheses [0] = 1;
withParentheses [1] = 2;

在第二种情况下,您没有这种自由.您必须在数组中定义一些初始值(已完成).
Nothing, as far as your example is concerned.

In the first case, you could actually define the array count in the first statement but add values to it later.
E.g.
List<byte> withParentheses = new List<byte>[4];
withParentheses[0]=1;
withParentheses[1]=2;

In the second case, you don''t have that liberty. You must define some initial values in the array (as you have done).


这可能会有所帮助,

CSharp 3 0概述 [
It might be helpful,

CSharp 3 0overview[^]

:)


这篇关于带或不带括号的新列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆