Generic method & constraint

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • timor.super@gmail.com

    Generic method & constraint

    Hi group,

    please look at my generic method :

    static void Write<T>(IEnume rable<IEnumerab le<T>list)
    {
    foreach (IEnumerable<Tt s in list)
    {
    foreach (T t in ts)
    {
    Console.WriteLi ne(t);
    }
    Console.WriteLi ne();
    }
    }

    static void Main()
    {
    List<List<int>l ist = new List<List<int>> (new List<int>[]
    {new List<int>(new int[] {1, 2, 3}), new List<int>(new int[] {3, 4,
    5}), new List<int>(new int[] {4, 5, 6})});
    List<List<strin g>list2 = new List<List<strin g>>(new
    List<string>[] { new List<string>(ne w string[] { "ab", "bc" }), new
    List<string>(ne w string[] { "aa" }), new List<string>(ne w string[]
    { "bb" }) });
    Write(list.ToAr ray());
    Write(list2.ToA rray());
    }

    My purpose is to have an IEnumerable of IEnumerable (for example array
    of array) and write the content

    But I'm not able to apply constraint. (should I)

    Is it the good way to make such a method ? Is there a better way ?

    Thanks for your help,

    Best regard
  • Jon Skeet [C# MVP]

    #2
    Re: Generic method &amp; constraint

    On Feb 11, 11:15 am, timor.su...@gma il.com wrote:
    Hi group,
    >
    please look at my generic method :
    >
    static void Write<T>(IEnume rable<IEnumerab le<T>list)
    <snip>
    My purpose is to have an IEnumerable of IEnumerable (for example array
    of array) and write the content
    >
    But I'm not able to apply constraint. (should I)
    What constraint do you want to apply? Why do you want to apply it?

    Jon

    Comment

    • timor.super@gmail.com

      #3
      Re: Generic method &amp; constraint

      On 11 fév, 12:24, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
      On Feb 11, 11:15 am, timor.su...@gma il.com wrote:
      >
      Hi group,
      >
      please look at my generic method :
      >
      static void Write<T>(IEnume rable<IEnumerab le<T>list)
      >
      <snip>
      >
      My purpose is to have an IEnumerable of IEnumerable (for example array
      of array) and write the content
      >
      But I'm not able to apply constraint. (should I)
      >
      What constraint do you want to apply? Why do you want to apply it?
      >
      Jon
      Hi jon & marc,

      I want a constraint that makes the use only of
      IEnumerable<IEn umerableto my function.

      In fact, I would like to do something harder than a simple for each,
      but what I want is to be able to iterate through a collection of
      collection, that are IEnumerable

      That can be an string[] of string[] or a list<of list<>, ...

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Generic method &amp; constraint

        On Feb 11, 12:34 pm, timor.su...@gma il.com wrote:
        <snip>
        I want a constraint that makes the use only of
        IEnumerable<IEn umerableto my function.
        Sorry, I still don't understand. You've already got a sort of
        constraint just because the method takes IEnumerable<IEn umerable<T>>.

        What do you need to allow that isn't allowed at the moment, or what do
        you need to prohibit which isn't prohibited at the moment?

        Jon

        Comment

        • timor.super@gmail.com

          #5
          Re: Generic method &amp; constraint

          On 11 fév, 13:42, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
          On Feb 11, 12:34 pm, timor.su...@gma il.com wrote:
          <snip>
          >
          I want a constraint that makes the use only of
          IEnumerable<IEn umerableto my function.
          >
          Sorry, I still don't understand. You've already got a sort of
          constraint just because the method takes IEnumerable<IEn umerable<T>>.
          >
          What do you need to allow that isn't allowed at the moment, or what do
          you need to prohibit which isn't prohibited at the moment?
          >
          Jon
          after more thoughts,

          I think I would like to do something like this :

          static void Write<T, T1>(T1 list) where T1 :
          IEnumerable<IEn umerable<T>>
          {
          foreach (IEnumerable<Te numerable in list)
          {
          foreach (T t in enumerable)
          {
          Console.WriteLi ne(t);
          }
          Console.WriteLi ne();
          }
          }

          but ...
          how to call it with a List<List<int>?

          Comment

          • Marc Gravell

            #6
            Re: Generic method &amp; constraint

            But why do you need a constraint? It is working fine as it is? By the
            way, the code can be written more cleanly in C# 3 (note I've mixed
            some List<Tand some T[] to check it works for any permutation):

            static void Main() {
            // test with a list of arrys
            var list = new List<int[]{
            new [] {1, 2, 3},
            new [] {3, 4, 5},
            new [] {4, 5, 6}
            };
            // test with an array of lists
            var list2 = new [] {
            new List<string{ "ab", "bc" },
            new List<string{ "aa" },
            new List<string{ "bb" }
            };
            Write(list.ToAr ray());
            Write(list2.ToA rray());
            }

            So: what isn't working? What is the issue?

            Marc


            Comment

            • Marc Gravell

              #7
              Re: Generic method &amp; constraint

              But *why* do you want to do that? What you have works!???
              how to call it with a List<List<int>?
              As per the code you posted originally? Or the slightly simpler C# 3
              equiavalent.

              Marc


              Comment

              • Marc Gravell

                #8
                Re: Generic method &amp; constraint

                What you have works!???

                Oops; I've just tried in 2005 (I use 2008 normally) - and the
                type-inference differences (not to mention the LINQ "ToArray") are
                making a difference. Apologies for confusion if you are using 2005
                (and the message that appears in 2005 is not necessarily obvious).

                So; either specify the type-arguments manually, or use C# 3 which does
                a better job here...

                With the signature:
                static void Write<TList,T>( IEnumerable<TLi stlist) where TList :
                IEnumerable<T{. ..}

                you can use (in VS2005):

                Write<List<int> ,int>(list);
                Write<List<stri ng>,string>(lis t2);


                Comment

                • timor.super@gmail.com

                  #9
                  Re: Generic method &amp; constraint

                  On 11 fév, 14:36, "Marc Gravell" <marc.grav...@g mail.comwrote:
                  What you have works!???
                  >
                  Oops; I've just tried in 2005 (I use 2008 normally) - and the
                  type-inference differences (not to mention the LINQ "ToArray") are
                  making a difference. Apologies for confusion if you are using 2005
                  (and the message that appears in 2005 is not necessarily obvious).
                  >
                  So; either specify the type-arguments manually, or use C# 3 which does
                  a better job here...
                  >
                  With the signature:
                  static void Write<TList,T>( IEnumerable<TLi stlist) where TList :
                  IEnumerable<T{. ..}
                  >
                  you can use (in VS2005):
                  >
                  Write<List<int> ,int>(list);
                  Write<List<stri ng>,string>(lis t2);
                  Ok, I understand better

                  thanks all for your answer

                  Best regard

                  Comment

                  • Marc Gravell

                    #10
                    Re: Generic method &amp; constraint

                    (note to self: check what happens in VS2005 when people ask about
                    generics)

                    If you need more clarification, please let me know,

                    Marc


                    Comment

                    Working...