Define an Extension Method for IEnumerable which returns IEnumerable?

后端 未结 3 1685
情话喂你
情话喂你 2021-01-31 08:54

How do I define an Extension Method for IEnumerable which returns IEnumerable? The goal is to make the Extension Method available for

3条回答
  •  余生分开走
    2021-01-31 09:39

    using System;
    using System.Collections.Generic;
    
    namespace ExtentionTest {
        class Program {
            static void Main(string[] args) {
    
                List BigList = new List() { 1,2,3,4,5,11,12,13,14,15};
                IEnumerable Smalllist = BigList.MyMethod();
                foreach (int v in Smalllist) {
                    Console.WriteLine(v);
                }
            }
    
        }
    
        static class EnumExtentions {
            public static IEnumerable MyMethod(this IEnumerable Container) {
                int Count = 1;
                foreach (T Element in Container) {
                    if ((Count++ % 2) == 0)
                        yield return Element;
                }
            }
        }
    }
    

提交回复
热议问题