Converting a IEnumberable sequence into an array - an example here

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raylopez99

    #1

    Converting a IEnumberable sequence into an array - an example here

    Here is a short program demonstrating using IEnumberable, Linq,
    predicates, extension methods and some tricks and how to convert an
    IEnumberable sequence into an array.

    For future reference, not intended as a question.

    RL

    Main():
    int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11};

    foreach (int i in numbers)
    { Console.Write(" {0} ", i); }

    Console.WriteLi ne("");


    double average_numbers _here = numbers.Average (); //
    extension method

    Console.WriteLi ne("average numbers: {0}",
    average_numbers _here);

    int[] numbers2;
    Predicate<intis Aeven = (delegate(int x) { return x % 2 ==
    0; });
    Console.WriteLi ne("Even(t)?: {0}, Even(f)?: {1}",
    isAeven(numbers[0]), isAeven(numbers[1])); //predicate logic

    IEnumerable<int numbers3 = numbers.Reverse (); //key, must
    defined IEnumerable as such, not really an array but can be made into
    one see **&&** below, thus int[] WillNotWorkArra y =
    numbers.Reverse (); //will not work directly, but you must do **&&**
    below

    Console.WriteLi ne("reverse");
    foreach (int i3 in numbers3)
    {
    Console.Write(" {0} ", i3);
    }
    numbers3 = numbers3.Revers e(); //works fine
    Console.WriteLi ne("\n Re-reverse");
    foreach (int i32 in numbers3)
    {
    Console.Write(" {0} ", i32);
    }

    numbers3 = numbers3.Revers e().Where(n =n % 2 == 0); //
    reverse and use even ints only

    int ACountVar_of_nu mbers3IEnumbera ble = numbers3.Count( ); //
    store # of elements of numbers3 IEnumberable sequence
    int ACountV = ACountVar_of_nu mbers3IEnumbera ble; //shorten
    the variable name

    int[] arrayNumbers3 = new int[ACountV];

    int arrayNumbers3_i nt = 0;
    Console.WriteLi ne("\n ...");
    foreach (int jfill in numbers3)
    {
    arrayNumbers3[arrayNumbers3_i nt] = jfill; //**&&**
    arrayNumbers3_i nt++;
    }


    for (int ie = 0; ie < ACountV; ie++)
    {
    Console.Write(" !: {0},", arrayNumbers3[ie]);

    }

    Console.WriteLi ne("\n Even only, reversed again");
    foreach (int i32 in numbers3)
    {
    Console.Write(" {0} ", i32);
    }

    Console.WriteLi ne("count is: {0}",
    ACountVar_of_nu mbers3IEnumbera ble);

    Output:
    0 1 2 3 4 5 6 7 8 9 10 11
    average numbers: 5.5
    Even(t)?: True, Even(f)?: False
    reverse
    11 10 9 8 7 6 5 4 3 2 1 0
    Re-reverse
    0 1 2 3 4 5 6 7 8 9 10 11
    ...
    !: 10, !: 8, !: 6, !: 4, !: 2, !: 0,
    Even only, reversed again
    10 8 6 4 2 0 count is: 6
    Press any key to continue . .

  • Peter Morris

    #2
    Re: Converting a IEnumberable sequence into an array - an example here

    int[] numbers2;
    Predicate<intis Aeven = (delegate(int x) { return x % 2 ==
    0; });
    Instead of:
    Predicate<intis Aeven = (delegate(int x) { return x % 2 == 0; });

    You could:
    Predicate<intis Aeven = i =i % 2 == 0;


    I am a little annoyed that I can do this

    List<intnumbers = ............;
    numbers.ForEach (.........);

    but I cannot do this

    int[] numbers = ............... ....;
    numbers.ForEach (.......);

    Can anyone tell me why this is? I don't mean why I can't do it, I mean why
    it may have been decided to implement in such a way that I can't.



    --
    Pete
    ====



    Comment

    • Jeroen Mostert

      #3
      Re: Converting a IEnumberable sequence into an array - an examplehere

      Peter Morris wrote:
      > int[] numbers2;
      > Predicate<intis Aeven = (delegate(int x) { return x % 2 ==
      >0; });
      >
      Instead of:
      Predicate<intis Aeven = (delegate(int x) { return x % 2 == 0; });
      >
      You could:
      Predicate<intis Aeven = i =i % 2 == 0;
      >
      >
      I am a little annoyed that I can do this
      >
      List<intnumbers = ............;
      numbers.ForEach (.........);
      >
      but I cannot do this
      >
      int[] numbers = ............... ....;
      numbers.ForEach (.......);
      >
      Can anyone tell me why this is? I don't mean why I can't do it, I mean
      why it may have been decided to implement in such a way that I can't.
      >
      Most operations on arrays take the form of static methods of Array,
      ..ForEach() is no exception. Arrays are special that way.

      In any case, Array.ForEach() and List<T>.ForEach () existed before LINQ. If
      you ask me they're pretty much useless, since they're no clearer than a
      foreach statement. Maybe if your language doesn't support foreach, but
      otherwise the only appeal is turning three lines into one -- and you could
      actually do that with the foreach statement too.

      --
      J.

      Comment

      Working...