How to create dynamic incrementing variable using “for” loop in C#

回眸只為那壹抹淺笑 提交于 2019-12-30 07:29:26

问题


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


回答1:


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));
    ...
}



回答2:


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
    ...
}



回答3:


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
}



回答4:


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;
    }
}



回答5:


Do as follow:

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



回答6:


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);
    ...
}


来源:https://stackoverflow.com/questions/7386698/how-to-create-dynamic-incrementing-variable-using-for-loop-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!