示例#1
0
            public TestObject(string name)
            {
                Name            = name;
                Number          = 42;
                Enumerable      = new EnumerableObject("Object 1", 2, new object(), 8743L);
                EmptyEnumerable = new EnumerableObject();
                List            = new List <string>()
                {
                    "List1", "List2", "List3"
                };
                Collection = new List <object>()
                {
                    "Collection1", 13245, new List <string>()
                    {
                        "CollSub1", "CollSub2"
                    }, new List <string>()
                };
                OtherValue             = 16;
                TestEnumeration        = TestEnum.Item3;
                TestFlagEnumeration    = TestFlags.Item1 | TestFlags.Item2;
                TestFlagEnumerationTwo = TestFlags.All;
                TestWithSameName       = new TestWithSameName();
                TestDictionary         = new Dictionary <int, string>()
                {
                    { 123, "Value One" }, { 234, "Value Two Three Four" }
                };
                TestDictionary2 = new Dictionary <int, TestWithSameName>()
                {
                    { 1, new TestWithSameName()
                      {
                          Name = "Nothing"
                      } },
                    { 2, new TestWithSameName()
                      {
                          Name = "Much"
                      } }
                };

                SameName = new TestWithSameName()
                {
                    Name = "ThirdLevel"
                };
            }
示例#2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Program extension method examples.");

            Data data = new Data();

            IEnumerable <int> evens;

            // Get a list of even number from existing data list of randome ints.
            // We can call an extension method directly by just calling it as normal.
            //  In that case we need to provide the THIS object as the first parameter.
            evens = EnumerableObject.FilterList(data.numberList, (intNumber) => { return(intNumber % 2 == 0); });

            // We can als call the extension method directly on the collection IEnumerable.
            evens = data.numberList.FilterList((intNumber) => { return(intNumber % 2 == 0); });
            evens = data.numberList.FilterList((i => i % 2 == 0));

            // This requires an IEnuerable<string> implementation of the ext method.
            evens.EnumToConsole();

            Console.WriteLine("Program finished. Press any key to exit.");
            Console.ReadKey();
        }