填充整数在.NET名单 [英] Populating a list of integers in .NET

查看:114
本文介绍了填充整数在.NET名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要整数从1到x,其中x是由用户设置的列表。我可以用它构建循环,例如假定x是一个整数集previously:

I need a list of integers from 1 to x where x is set by the user. I could build it with a for loop eg assuming x is an integer set previously:

List<int> iList = new List<int>();
for (int i = 1; i <= x; i++)
{
    iList.Add(i);
}

这似乎是愚蠢的,肯定有一个更优雅的方式来做到这一点,有点像 PHP范围的方法

This seems dumb, surely there's a more elegant way to do this, something like the PHP range method

推荐答案

如果您正在使用.net 3.5,<一个href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx">Enumerable.Range是你所需要的。

If you're using .Net 3.5, Enumerable.Range is what you need.

生成的积分序列   在指定范围内的号码。

Generates a sequence of integral numbers within a specified range.

这篇关于填充整数在.NET名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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