コード例 #1
0
ファイル: DelegatesTesting.cs プロジェクト: derikos/seeSharp
        public void TestDelegates()
        {
            StaticTesting staticTesting = new StaticTesting();

            //clasic way to assign a new delegate
            // pointToMethod myDelegate = new pointToMethod(StaticTesting.PrintName);

            //compiler does work behind the scene as the above example
            //pointToMethod myDelegate = StaticTesting.PrintName;

            //can add multiple methods to the delegates' invocation list
            //myDelegate += StaticTesting.PrintName;
            //myDelegate("Alex");

            //pointToMethod myDelegate = StaticTesting.ReturnStaticStringTwo;
            //myDelegate += StaticTesting.ReturnStaticStringOne;
            //string localString = myDelegate();
            //Console.WriteLine(myDelegate());


            //pointToDog myDogDelegate = this.GetDoge;
            //pointToAnimal myAnimalDelegate = this.doVet;

            //CountIt myCountDelegate = TestCounting;
            //myCountDelegate();

            //or with anonymous founction
            //CountIt myAnonymousDelegate = delegate
            //{
            //    for (int i = 0; i < 5; i++)
            //    {
            //        Console.WriteLine(i);
            //    }
            //};
            //myAnonymousDelegate();

            //AddIt myLambdaDelegate = (int x) => x + 5; //statement lambda


            //expression lambda
            AddIt myLambdaDelegate = (int x) =>
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine(i);
                }
                Console.WriteLine("Lambda loop ended");
                return(x + 5);
            };



            int result = myLambdaDelegate(5);

            Console.WriteLine(result);

            Console.ReadKey();
        }
コード例 #2
0
        public void StaticTestingEngine()
        {
            int           testLocalStaticEnginge = 0;
            StaticTesting localStatic            = new StaticTesting();

            testLocalStaticEnginge = localStatic.GetNoOfInstances();
            Console.WriteLine("Static with one object : " + testLocalStaticEnginge);
            List <StaticTesting> staticListTesting = new List <StaticTesting>();

            for (int i = 0; i < 10; i++)
            {
                staticListTesting.Add(new StaticTesting());
            }
            Console.WriteLine("Static with List added: " + StaticTesting.StaticMethodInstances());
        }