发电机组整数在C# [英] Generating sets of integers in C#

查看:99
本文介绍了发电机组整数在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在F#中,你可以生成一组数字,只需说[1..100]。

In F#, you can generate a set of numbers, just by saying [1..100].

我想在C#中做类似的事情。这是我到目前为止:

I want to do something similar in C#. This is what I have come up with so far:

public static int[] To(this int start, int end)
{
    var result = new List<int>();
    for(int i = start; i <= end; i++)
        result.Add(i);
    return result.ToArray();
}



通过这样做,我现在可以通过说1.To 100)

By doing this, I can now create a set by saying 1.To(100)

不幸的是,这不像[1..100]那样可读。有没有人想出一个更好的方法来做这个在C#?如果它是小写的,它是更可读的吗? 1.到(100),例如?或者,是到一个坏词?是类似1.Through(100)更可读吗?

Unfortunately, this is not nearly as readable as [1..100]. Has anyone come up with a better way to do this in C#? Is it more readable if it is lowercase? 1.to(100), for instance? Or, is "To" a bad word? Is something like 1.Through(100) more readable?

只是寻找一些想法。有没有人想出一个更优雅的解决方案?

Just looking for some thoughts. Has anyone else come up with a more elegant solution?

编辑:
阅读完回复后,方法使用范围:

After reading the responses, I have re-written my To method using the range:

public static int[] To(this int start, int end)
{
    return Enumerable.Range(start, end - start + 1).ToArray();
}


$ b $ p我仍然在寻找关于1.To的可读性的想法

I am still looking for thoughts on the readability of 1.To(100)

推荐答案

Enumerable.Range(1,100);

Enumerable.Range(1, 100);

这篇关于发电机组整数在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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