none
Foreach RRS feed

  • Question

  • Hi guys,

    what if the foreach statement can do this?

    foreach( int index; var obj in manyObjects){

     dosomething  = obj;

    dosomething.Index= index;

    }

    Tuesday, December 15, 2015 7:57 AM

Answers

  • Hi schüky,

    In C#, you cannot define a parameter in foreach statement inside.

    Please refer to the following code,

     int index;
                foreach(  var obj in manyObjects){
    
     dosomething  = obj;
    
    dosomething.Index= index;
    
    }

    Habe a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 15, 2015 10:04 AM
  • Hi shuky,

    Kristin provided a great example, but if you need to increment the index after each iteration all I would add is index++; after the last line, making it..

    int index;
    foreach(var obj in manyObjects)
    {
       dosomething = obj;
       dosomething.Index = index;
       index++;
    }

    Best of luck.

    Tuesday, December 15, 2015 1:51 PM
  • I think he wants a foreach with but still have the index available:

    not possible, use for (int i = 0; i < list.count; i++)


    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.

    Tuesday, December 15, 2015 3:25 PM
  • If you like more alternative ways, not necessary concise and optimal, then:

        manyobjects

           .Select( ( obj, i ) => obj.Index = i )

           .ToList();

    Or

        foreach( var _ in manyobjects.Select( ( obj, i ) => obj.Index = i ));

    Tuesday, December 15, 2015 7:01 PM

All replies

  • Hi schüky,

    In C#, you cannot define a parameter in foreach statement inside.

    Please refer to the following code,

     int index;
                foreach(  var obj in manyObjects){
    
     dosomething  = obj;
    
    dosomething.Index= index;
    
    }

    Habe a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 15, 2015 10:04 AM
  • Hi shuky,

    Kristin provided a great example, but if you need to increment the index after each iteration all I would add is index++; after the last line, making it..

    int index;
    foreach(var obj in manyObjects)
    {
       dosomething = obj;
       dosomething.Index = index;
       index++;
    }

    Best of luck.

    Tuesday, December 15, 2015 1:51 PM
  • Does this really make sense?

    foreach depends on GetEnumerator. It does not necessarily return the objects in the order defined by the index property of your class. Thus this could result in a re-indexing in a "random" order.

    What is your use-case?

    Tuesday, December 15, 2015 1:55 PM
  • I think he wants a foreach with but still have the index available:

    not possible, use for (int i = 0; i < list.count; i++)


    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.

    Tuesday, December 15, 2015 3:25 PM
  • It cannot and therefore not a subject of this forum. 

    This forum is not done by Microsoft developers. 

    For suggestion take the Visual Studio Suggestions website

    https://visualstudio.uservoice.com


    Success
    Cor

    Tuesday, December 15, 2015 4:46 PM
  • If you like more alternative ways, not necessary concise and optimal, then:

        manyobjects

           .Select( ( obj, i ) => obj.Index = i )

           .ToList();

    Or

        foreach( var _ in manyobjects.Select( ( obj, i ) => obj.Index = i ));

    Tuesday, December 15, 2015 7:01 PM