自动初始化C#列表 [英] Auto-initializing C# lists

查看:179
本文介绍了自动初始化C#列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个新的C#列表(名单,其中,双> )。有没有一种方法,除了做一个遍历目录,初始化所有的起始值为0?

I am creating a new C# List (List<double>). Is there a way, other than to do a loop over the list, to initialize all the starting values to 0?

推荐答案

在除了提供功能性解决方案(使用的可枚举类的静态方法),你可以传数组的形式在构造函数中。

In addition to the functional solutions provided (using the static methods on the Enumerable class), you can pass an array of doubles in the constructor.

var tenDoubles = new List<double>(new double[10]);

这工作,因为一个企业的默认值已经是0,而且很可能执行稍微好一点。

This works because the default value of an double is already 0, and probably performs slightly better.

这篇关于自动初始化C#列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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