none
C# IEnumerable<IGrouping<TKey, TElement>> RRS feed

  • Question

  • Hello,

    Although i can use var keyword but i want used explicit data type.

    I know IEnumerable<T> but I can't imagine well and understanding ' IEnumerable<IGrouping<TKey, TElement>>' :(


            static void Main(string[] args)
            {
                IEnumerable<IGrouping<string, Customer>> groupQuery =
                    from c in customers
                    group c by c.State;
    
                IEnumerable<IGrouping<bool, Customer>> groupPriceQuery =
                    from c in customers
                    group c by c.Price >= 1000;
    
                foreach (IGrouping<bool, Customer> priceGroup in groupPriceQuery)
                {
                    Console.WriteLine("{0}", priceGroup.Key);
                    foreach (Customer c in priceGroup)
                    {
                        Console.WriteLine("  {0} {1}: {2}", c.First, c.Last, c.Price);
                    }
                }
    
                Console.ReadKey();
            }


    For simplicity and simulation of IEnumerable<IGrouping<TKey, TElement>>  I wrote simple class. Now How can return IA<IB<T>> in my example?

    using System;
    
    namespace ConsoleApp9
    {
        interface IA<TSource>
        {
            void AMethod(int a);
        }
        interface IB : IA<int>
        {
            void BMethod(string str);
        }
        class Program : IA<int>
        {
    
            static void Main(string[] args)
            {
                IA<int> a = new Program();
            }
    
            public void AMethod(int a)
            {
                Console.WriteLine("IA Interface");
            }
        }
    }
    


    • Edited by Arash_89 Saturday, March 31, 2018 10:34 PM
    Saturday, March 31, 2018 9:48 PM

Answers

  • Hello Arash_89,

    >>For simplicity and simulation of IEnumerable<IGrouping<TKey, TElement>>  I wrote simple class. Now How can return IA<IB<T>> in my example?

    For your example it couldn't return a IA<IB<T>> object because it doesn't exist IB generic interface. If you want to understand type structure of IEnumerable<IGrouping<TKey, TElement>>,  you could press f12 to look up current object inheriting structure. A simple code like below.

      namespace ConsoleApp9
        {
            interface IA<T>
            {
                void AMethod(T a);
            }
    
            interface IB<T>:IA<T>
            {
                void BMethod(T str);
            }
            class Te<T> : IB<T>
            {
                public void AMethod(T a)
                {               
                }
    
                public void BMethod(T str)
                {             
                }
            }
    
            class Program
            {
                static void Main(string[] args)
                {
                    IB<IA<int>> ds = new Te<IA<int>>();
                }
               
            }
        }
     

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arash_89 Monday, April 2, 2018 12:03 PM
    Monday, April 2, 2018 5:21 AM
    Moderator