如何使用“for”创建动态递增变量C# 中的循环

发布于 2024-12-03 21:02:35 字数 89 浏览 0 评论 0原文

如何在 C# 中使用“for”循环创建动态递增变量?像这样: track_1、track_2、track_3、track_4。依此类推。

How to create dynamic incrementing variable using "for" loop in C#? like this:
track_1, track_2, track_3, track_4. so on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

薄荷梦 2024-12-10 21:02:35

您无法创建动态命名的变量。您所能做的就是创建一些集合或数组,并对其进行操作。
我认为最适合您的课程是通用 List

List<String> listWithDynamic = new List<String>();
for (int i = 1; i < limit; i +=1)
{
    listWithDynamic.Add(string.Format("track_{0}", i));
    ...
}

You can't create dynamically-named variables. All you can do - it to create some collection or array, and operate with it.
I think the best class for you is generic List<>:

List<String> listWithDynamic = new List<String>();
for (int i = 1; i < limit; i +=1)
{
    listWithDynamic.Add(string.Format("track_{0}", i));
    ...
}
情定在深秋 2024-12-10 21:02:35

假设您想要字符串:

for (int i = 1; i < limit; i +=1)
{
    string track = string.Format("track_{0}", i);
    ...
}

但是当您已经有名为 track_1、track_2、track_3、track_4 的变量时,您将需要一个数组或列表:

var tracks = new TrackType[] { track_1, track_2, track_3, track_4 } ;

for (int i = 0; i < tracks.length; i++)
{
    var track = tracks[i];  // tracks[0] == track_1
    ...
}

Assuming you want strings:

for (int i = 1; i < limit; i +=1)
{
    string track = string.Format("track_{0}", i);
    ...
}

But when you already have variables called track_1, track_2, track_3, track_4 you will need an array or List:

var tracks = new TrackType[] { track_1, track_2, track_3, track_4 } ;

for (int i = 0; i < tracks.length; i++)
{
    var track = tracks[i];  // tracks[0] == track_1
    ...
}
枕梦 2024-12-10 21:02:35

显而易见的解决方案

for (var i = 0; i < 10; i++)
{
    var track = string.Format("track_{0}", i);

}

基于 Linq 的解决方案

foreach (var track in Enumerable.Range(0, 100).Select(x => string.Format("track_{0}", x)))
{

}

基于运算符的解决方案 这有点 hacky,但仍然很有趣。 >

for (var i = new Frob(0, "track_{0}"); i < 100; i++)
{
    Console.WriteLine(i.ValueDescription);
}

struct Frob
{
    public int Value { get; private set; }
    public string ValueDescription { get; private set; }
    private string _format;

    public Frob(int value, string format)
        : this()
    {
        Value = value;
        ValueDescription = string.Format(format, value);
        _format = format;
    }

    public static Frob operator ++(Frob value)
    {
        return new Frob(value.Value + 1, value._format);
    }

    public static Frob operator --(Frob value)
    {
        return new Frob(value.Value - 1, value._format);
    }

    public static implicit operator int(Frob value)
    {
        return value.Value;
    }

    public static implicit operator string(Frob value)
    {
        return value.ValueDescription;
    }

    public override bool Equals(object obj)
    {
        if (obj is Frob)
        {
            return ((Frob)obj).Value == Value;
        }
        else if (obj is string)
        {
            return ((string)obj) == ValueDescription;
        }
        else if (obj is int)
        {
            return ((int)obj) == Value;
        }
        else
        {
            return base.Equals(obj);
        }
    }

    public override int GetHashCode()
    {
        return Value;
    }

    public override string ToString()
    {
        return ValueDescription;
    }
}

Obvious Solution

for (var i = 0; i < 10; i++)
{
    var track = string.Format("track_{0}", i);

}

Linq-Based Solution

foreach (var track in Enumerable.Range(0, 100).Select(x => string.Format("track_{0}", x)))
{

}

Operator-Based Solution This is somewhat hacky, but fun none-the-less.

for (var i = new Frob(0, "track_{0}"); i < 100; i++)
{
    Console.WriteLine(i.ValueDescription);
}

struct Frob
{
    public int Value { get; private set; }
    public string ValueDescription { get; private set; }
    private string _format;

    public Frob(int value, string format)
        : this()
    {
        Value = value;
        ValueDescription = string.Format(format, value);
        _format = format;
    }

    public static Frob operator ++(Frob value)
    {
        return new Frob(value.Value + 1, value._format);
    }

    public static Frob operator --(Frob value)
    {
        return new Frob(value.Value - 1, value._format);
    }

    public static implicit operator int(Frob value)
    {
        return value.Value;
    }

    public static implicit operator string(Frob value)
    {
        return value.ValueDescription;
    }

    public override bool Equals(object obj)
    {
        if (obj is Frob)
        {
            return ((Frob)obj).Value == Value;
        }
        else if (obj is string)
        {
            return ((string)obj) == ValueDescription;
        }
        else if (obj is int)
        {
            return ((int)obj) == Value;
        }
        else
        {
            return base.Equals(obj);
        }
    }

    public override int GetHashCode()
    {
        return Value;
    }

    public override string ToString()
    {
        return ValueDescription;
    }
}
入怼 2024-12-10 21:02:35

不知道我是否明白你的问题,但我会尝试:

for(var i = 1; i < yourExclusiveUpperbound; i++)
{
   var track = String.Format("$track_{0}", i);
   // use track
}

或者使用一些 LINQ-Magic:

foreach(var track in Enumerate.Range(1, count)
                              .Select(i => String.Format("$track_{0}", i)))
{
   // use track
}

don't know if I get your question, but I will try:

for(var i = 1; i < yourExclusiveUpperbound; i++)
{
   var track = String.Format("$track_{0}", i);
   // use track
}

or with some LINQ-Magic:

foreach(var track in Enumerate.Range(1, count)
                              .Select(i => String.Format("$track_{0}", i)))
{
   // use track
}
眼泪淡了忧伤 2024-12-10 21:02:35

执行如下操作:

for (int i = 0; i < lenght; i ++)
{
    any work do in loop
}

Do as follow:

for (int i = 0; i < lenght; i ++)
{
    any work do in loop
}
尾戒 2024-12-10 21:02:35

不,我们不能在循环中创建动态命名的变量。但是,还有其他优雅的方法来解决该问题,而不是创建动态命名变量。

一种可能是,在循环之前创建一个数组或列表,并将值存储在循环中的数组/列表项中。您可以稍后在代码中的任何位置访问数组/列表。如果您知道要使用哪个变量(track_1,track_2,...),您可以简单地从数组/列表(tracks[1],tracks[2],...)访问它。

List<String> tracks = new List<String>();
for (int i = 1; i < limit; i++)
{
    Track track = new Track();
    tracks.Add(track);
    ...
}

No, we can't create dynamically named variables in a loop. But, there are other elegant ways to address the problem instead of creating dynamically named variables.

One could be, create an array or list before the loop and store values in array / list items in the loop. You can access the array / list later anywhere in your code. If you know which variable you want to use (track_1, track_2, ...), you can simply access it from the array / list (tracks[1], tracks[2], ...).

List<String> tracks = new List<String>();
for (int i = 1; i < limit; i++)
{
    Track track = new Track();
    tracks.Add(track);
    ...
}
放低过去 2024-12-10 21:02:35
  for (int i=5;i<30;(i%2==0)?i=i+4:i=i+2) {
     cout<<i;
     cout<<endl;
   }

使用三元运算符会有所帮助。

  for (int i=5;i<30;(i%2==0)?i=i+4:i=i+2) {
     cout<<i;
     cout<<endl;
   }

Using ternary operator can help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文