请告诉我创建一个新的名单,LT这三种方式的区别;串>在C#中? [英] Whats the difference between these three ways of creating a new List<string> in C#?
本文介绍了请告诉我创建一个新的名单,LT这三种方式的区别;串>在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我创建一个新的名单,LT这三种方式的区别;串>在C#
A =新的List<串GT;();
B =新的List<串GT; {};
C =新的List<串GT;(){};
解决方案
这些都是等价的,所有这三个创建一个空的列表与LT;字符串方式>
- 是一个简单的,无参数的构造函数
- 使用集合初始化语法,在那里你被允许省略构造函数的括号如果类提供参数的构造函数
- 同2,只是提供了可选构造支撑。
集合初始化语法允许您提供在构造对象数据,
列表<串GT;名单=新名单,LT;字符串> { 一二三 };
被扩展到
列表与LT;字符串>名单=新名单,LT;串>();
list.Add(1);
list.Add(二);
list.Add(三);
由编译器。
Whats the difference between these three ways of creating a new List<string>
in C#?
A = new List<string>();
B = new List<string> { };
C = new List<string>() { };
解决方案
Those are equivalent, all three create an empty List<string>
.
- is a simple, parameterless constructor
- uses the collection initialization syntax, where you are allowed to omit the braces of the constructor if the class provides a parameterless constructor
- the same as 2., just providing the optional constructor braces.
The collection initialization syntax allows you to provide data while constructing the object,
List<string> list = new List<string> { "one", "two", "three" };
gets expanded to
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
list.Add("three");
by the compiler.
这篇关于请告诉我创建一个新的名单,LT这三种方式的区别;串>在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文