示例#1
0
        static void Main()
        {
            // Delegate instantiation with method "DoubleAmount".
            MathAction ma = DoubleAmount;

            // Calls delegate ma that is associated with method "DoubleAmount".
            double multByTwo = ma(4.5);

            Console.WriteLine("multByTwo: {0}", multByTwo);

            // Instantiate a delegate associate with an anonymous method
            // that returns squared amount of an input with return type double.
            MathAction ma2 = delegate(double input)
            {
                return(input * input);
            };

            // Calls ma2 delegate associate with an anonymous method that
            // returns squared amount of an input.
            double square = ma2(5);

            Console.WriteLine("square: {0}", square);

            // Instantiate a delegate with a lambda expression.
            // A delegate can be instantiated with a method or a lambda expression.
            // The following lambda expression returns cubed amount of an input.
            MathAction ma3  = s => s * s * s;
            double     cube = ma3(4.375);

            Console.WriteLine("cube: {0}", cube);
        }
示例#2
0
        static void Main()
        {
            // Instantiate delegate with named method:
            MathAction ma = Double;

            // Invoke delegate ma:
            double multByTwo = ma(4.5);

            Console.WriteLine("multByTwo: {0}", multByTwo);

            // Instantiate delegate with anonymous method:
            MathAction ma2 = delegate(double input)
            {
                return(input * input);
            };

            double square = ma2(5);

            Console.WriteLine("square: {0}", square);

            // Instantiate delegate with lambda expression
            MathAction ma3  = s => s * s * s;
            double     cube = ma3(4.375);

            Console.WriteLine("cube: {0}", cube);
        }
示例#3
0
文件: Program.cs 项目: sarsembin/PP2
        public static void del_ex(object state)
        {
            MathAction ma    = first_delegate.sum;
            double     summa = ma(2, 3);

            Console.WriteLine("Sum:" + summa);
        }
示例#4
0
        static void Main()
        {
            // 1.Instatiate delegate with anonymous method
            MathAction ma = delegate(double input)
            {
                return(input * input);
            };

            double square = ma(5);

            System.Console.WriteLine("square: {0}", square);

            //--------------------------------------------------->
            //2. Instantiate delegate with lambda expression
            MathAction ma2  = s => s * s * s;
            double     cube = ma2(3);

            System.Console.WriteLine("square1: {0}", cube);

            //--------------------------------------------------->
            //3. Instantiate delegate with named method: Point declaration of MathAction to named method
            MathAction ma3 = DoubleD;

            ma3 += TrippleD;

            // Invoke delegate ma:
        }
示例#5
0
        static void Main(string[] args)
        {
            // Instantiate/Encapsulating named method
            MathAction maVar = Double;

            double multByTwo = maVar(4.5);

            Console.WriteLine($"The number 4.5 multiplied by two: {multByTwo}.");

            // Instantiate/Encapsulating anonymous method
            MathAction maVar2 = delegate(double input)
            {
                return(input * input);
            };
            double squareTheNumber = maVar2(10);

            Console.WriteLine($"The number 10 squared: {squareTheNumber}.");

            // Instantiate delegate with lambda expression
            MathAction maVar3        = c => c * c * c;
            double     cubeTheNumber = maVar3(7.885);

            Console.WriteLine($"The number 7.885 cubed: {cubeTheNumber}.");

            Console.ReadLine();
        }
示例#6
0
        private static void testdelegate()
        {
            // 3.藉由方法名稱 Instantiate delegate
            MathAction ma = Double;

            // 4.呼叫 delegate ma
            double multByTwo = ma(4.5);

            Console.WriteLine("multByTwo: {0}", multByTwo);

            // 3.藉由匿名方法 Instantiate delegate
            MathAction ma2 = delegate(double input)
            {
                return(input * input);
            };
            // 4.呼叫 delegate square
            double square = ma2(5);

            Console.WriteLine("square: {0}", square);

            // 3.藉由lambda expression Instantiate delegate
            MathAction ma3 = s => s * s * s;
            // 4.呼叫 delegate cube
            double cube = ma3(4.375);

            Console.WriteLine("cube: {0}", cube);
            // 輸出:
            // multByTwo: 9
            // square: 25
            // cube: 83.740234375
        }
示例#7
0
        static void Main(string[] args)
        {
            MathAction Add = (x, y) => { return(x + y); };
            MathAction Sub = (x, y) => { return(x - y); };
            MathAction Mul = (x, y) => { return(x * y); };
            MathAction Div = (x, y) =>
            {
                if (y != 0)
                {
                    return(x / y);
                }
                else
                {
                    Console.WriteLine("Mistake! Division by zero is not allowed");
                    return(0);
                }
            };

            bool isContinue = true;

            do
            {
                double number1 = InputDoubleNumber("number1");
                double number2 = InputDoubleNumber("number2");
                Console.Write("Select your action: '+', '-', '*', '/' or 'q' to exit: ");
                ConsoleKeyInfo key = Console.ReadKey();
                Console.WriteLine();
                switch (key.KeyChar)
                {
                case 'q':
                    isContinue = false;
                    break;

                case '+':
                    Console.WriteLine($"{number1} + {number2} = {Add(number1, number2)}");
                    break;

                case '-':
                    Console.WriteLine($"{number1} - {number2} = {Sub(number1, number2)}");
                    break;

                case '*':
                    Console.WriteLine($"{number1} * {number2} = {Mul(number1, number2)}");
                    break;

                case '/':
                    Console.WriteLine($"{number1} / {number2} = {Div(number1, number2)}");
                    break;

                default:
                    Console.WriteLine("Wrong action");
                    break;
                }
                Console.WriteLine();
            } while (isContinue);
        }
示例#8
0
        /// <summary>
        /// Constructor of a math operation DeMIMOI model
        /// </summary>
        /// <param name="mathAction">The math operation to execute</param>
        public MathOperation(MathAction mathAction)
            : base(null, new DeMIMOI_Port(1))
        {
            ma = mathAction;

            // Set the name of the operation to the DeMIMOI model
            Name = ma.Method.Name;

            // Initialise the first value
            Outputs[0][0].Value = ma(0);
        }
        static bool getAction()
        {
            Console.WriteLine();
            Console.Write("Your Choice: ");
            string action = Console.ReadLine();

            if (action.Contains("+"))
            {
                statement = MathAction.Plus;
            }
            else if (action.Contains("-"))
            {
                statement = MathAction.Minus;
            }
            else if (action.Contains("/"))
            {
                statement = MathAction.Divide;
            }
            else if (action.Contains("*"))
            {
                statement = MathAction.Multiply;
            }
            else if (action.Contains(">="))
            {
                statement = MathAction.GreaterOrEqual;
            }
            else if (action.Contains("<="))
            {
                statement = MathAction.LessOrEqual;
            }
            else if (action.Contains(">"))
            {
                statement = MathAction.Greater;
            }
            else if (action.Contains("<"))
            {
                statement = MathAction.Less;
            }
            else if (action.Contains("=="))
            {
                statement = MathAction.Equal;
            }
            else if (action.Contains("!="))
            {
                statement = MathAction.NotEqual;
            }

            return(statement == MathAction.Initial ? false : true);
        }
示例#10
0
        static void Main(string[] args)
        {
            MathAction ma1 = new MathAction(Double);
            MathAction ma2 = delegate(double num) {  //Anonymous Methods
                Console.WriteLine("addByThree...");
                return(num + 3);
            };
            MathAction ma3 = x => { //lambda expression
                Console.WriteLine("become100...");
                return(100);
            };
            MathAction ma4 = ma3 + ma1;
            ShowAction ma5 = ShowParams;
            ShowAction ma6 = delegate(double[] nums) {
                var sum = 0d;
                foreach (var num in nums)
                {
                    sum += num;
                }
                Console.WriteLine($"sum: {sum}");
            };
            //life cycle
            ShowAction ma7;
            {
                int y = 100;
                ma7 = nums => Console.WriteLine($"x: {y}");
            }
            //y = 1000;
            double multByTwo = ma1(4.5d);

            Console.WriteLine($"multByTwo: {multByTwo}");
            double addByThree = ma2(5d);

            Console.WriteLine($"addByThree: {addByThree}");
            double become100 = ma3(12d);

            Console.WriteLine($"become100: {become100}");
            double become100_and_multByTwo = ma4(4);

            Console.WriteLine($"become100_and_multByTwo: {become100_and_multByTwo}");
            ma5(1, 2, 3, 4, 5, 6, 7, 8, 9);
            ma6(1, 3, 5, 7);
            //Console.WriteLine($"x: {y}");//NG y is dead
            ma7();//y in ma7 is still alive
            Console.ReadKey();
        }
示例#11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter first operand:");
            int left = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter action:");
            string action = Console.ReadLine();

            Console.WriteLine("Enter second operand:");
            int right = Convert.ToInt32(Console.ReadLine());

            double result = 0;

            switch (action)
            {
            case "+":
                MathAction Add = (x, y) => x + y;
                result = Add(left, right);
                break;

            case "-":
                MathAction Sub = (x, y) => x - y;
                result = Sub(left, right);
                break;

            case "*":
                MathAction Mul = (x, y) => x * y;
                result = Mul(left, right);
                break;

            case "/":
                MathAction Div = (x, y) => y == 0 ? double.PositiveInfinity : x / y;
                result = Div(left, right);
                break;

            default:
                break;
            }

            Console.WriteLine("Result = " + result);
        }
示例#12
0
        static void Main(string[] args)
        {

            MathAction ma1 = new MathAction(Add);
            double result1 = ma1(5, 6);
            Console.WriteLine(result1);


            MathAction ma2 = delegate(double a, double b)
            {
                return a * b;
            };
            double result2 = ma2(7, 8);
            Console.WriteLine(result2);


            MathAction ma3 = (s, a) => (s * s * a * a);
            double result3 = ma3(4, 4);
            Console.WriteLine(result3);

        }
示例#13
0
        public void Test()
        {
            LearnDeligate c = new LearnDeligate();
            // LearnDeligate p = new LearnDeligate(); if this method is static, but the method to which the delegate points
            //is non static method.
            //1. Named Method
            // MathAction d1 = Double;

            //Another way to write/to assign value (another method) to delegate
            MathAction d1 = new MathAction(Double); // MathAction d1 = new MathAction(p.Double); if this method is static, but the method to which the delegate points

            //is non static method.
            Console.WriteLine("the delegate says {0}", d1(3.14));

            //2nd way to assign function as value to variable using  anonymous function/method
            //Anonymous Method/function. "delegate" is a keyword for defining anonymous functions
            d1 = delegate(double num)
            {
                return(num * num);
            };

            Console.WriteLine("the delegate says {0}", d1(3.14));

            //3th  way to assign value to delegate using  Lambda Expressions/Statements. Lambda is equal/actually anonymous function.
            d1 = x => x * x * x; //This is expression and expressions always are being evaluated to certain value;
            //Expression is a "word" in our language. Statement is "whole statement like
            //sentence/couple of words". E.g for statement - if-else construction
            // d1 = (x,y)=> { x++; return x * x * x; }; //this is lambda statement - {} always use return, cause it is unknow otherwise
            //what should be returned as a result from the statement. Lambda is in () if it has more than 1 param. But the delegate
            //and the function to which it is pointing should also be stated with more than one param.

            Console.WriteLine("the delegate says {0}", d1(3.14));

            //  FUnctional Developement
            Console.WriteLine("Functional Calculator: {0}", c.Calculate(d1, 2));

            // Console.WriteLine("Functional calculator {0}", p.Calculate(s => s * s, 2.71));

            // Console.WriteLine("Functional calculator {0}", p.Calculate(delegate(double num) { return num * num; }, 2.71));
        }
示例#14
0
        //在什么情况下使用委托

        public void delegateTest()
        {
            //把具体方法的指针给委托
            MathAction ma        = DelegateTest.Double;
            double     multByTwo = ma(4.5);

            Console.WriteLine("multByTwo:{0}", multByTwo);


            // Instantiate delegate with anonymous method:
            //实例化委托与匿名方法:

            //实例化委托
            MathAction ma2 = delegate(double input)
            {
                return(input * input);
            };

            double square = ma2(5);

            Console.WriteLine("square:{0}", square);


            //匿名委托
            MathAction ma3  = s => s * s * s;
            double     cube = ma3(4.375);

            Console.WriteLine("cube:{0}", cube);


            调试委托       委托对象 = new 调试委托();
            MathAction 新委托  = 委托参数 => 委托参数 * 1;
            MathAction 新委托2 = 委托对象.返回双精度浮点数的方法;

            新委托2(2);

            var 委托方法  = 新委托.Method;
            var 委托方法2 = 新委托2.Method;
        }
示例#15
0
        static void Main(string[] args)
        {
            MathAction m1 = new MathAction(Add);

            m1 += Sub;

            m1 += (x, y) => x * y;


            Console.WriteLine(m1.Invoke(2, 4));

            // Demonstrate invocation lists
            Delegate[] invocationList = m1.GetInvocationList();
            foreach (var del in invocationList)
            {
                int        result = 0;
                MathAction ma     = (MathAction)del;
                // ma(2, 4);

                if (ma.Method.Name.Equals("Add"))
                {
                    ;
                }
                else
                {
                    result = ma(2, 4);
                }
                Console.WriteLine(result);



                // if we do not know the type of the delegate
                // in compilation, we might want to use
                // DynamicInvoke which is more flexible than
                // specifically invoking a certain delegate
                //del.DynamicInvoke(new object[] { 2, 4 });
            }
        }
示例#16
0
        public void DelegateMethod()
        {
            MathAction ma         = Double;
            double     multiByTwo = ma(4.5);

            Console.WriteLine("multi By two : {0}", multiByTwo);

            //Instantiate delegate with anonymous method:
            MathAction ma2 = delegate(double input)
            {
                return(input * input);
            };

            double square = ma2(5);

            Console.WriteLine("squar : {0}", square);

            // Instance delgete with lambda expression.
            MathAction ma3  = s => s * s * s;
            double     cube = ma3(5);

            Console.WriteLine("cube : {0}", cube);
        }
示例#17
0
        static void Main(string[] args)
        {
            //Create an instance for delegate with named method
            MathAction m1 = Multiplication;
            //invoke the delegate instance
            double ans = m1(4.5);

            Console.WriteLine(ans);
            //Createing a second instance and performs square
            MathAction m2 = delegate(double input)
            {
                return(input * input);
            };
            double square = m2(5.375);

            Console.WriteLine(square);

            //create one more instance with lambda expression
            MathAction m3   = s => s * s * s;
            double     cube = m3(3.222);

            Console.WriteLine(cube);
            Console.Read();
        }
示例#18
0
        public static MyFraction MathOperationHandler(MyFraction num1, MyFraction num2, MathAction action)
        {
            MyFraction output = new MyFraction();

            switch (action)
            {
            case MathAction.ADD:
            case MathAction.SUBSTRACT:
                output = Utils.AddSubstractHandler(num1, num2, action);
                break;

            case MathAction.MULTIPLY:
            case MathAction.DIVIDE:
                output = Utils.MultiplyDivideHandler(num1, num2, action);
                break;

            default:
                break;
            }

            return(output);
        }
示例#19
0
 public static void DisplayResult(int n1, int n2, MathAction mathAction)
 {
     Console.WriteLine(mathAction(n1, n2));
 }
示例#20
0
        public void anonymousDelegate()
        {
            MathAction action = delegate(double input) { return(input * input); };

            Assert.AreEqual(25, action(5));
        }
示例#21
0
        public void callDelegateByReference()
        {
            MathAction action = Double;

            Assert.AreEqual(10, action(5));
        }
示例#22
0
        public static MyFraction MultiplyDivideHandler(MyFraction num1, MyFraction num2, MathAction action)
        {
            // preparing output variable
            MyFraction resultFraction = new MyFraction();

            // in case of division we simply reverse the second fraction
            if (action == MathAction.DIVIDE)
            {
                int nominator   = num2.Nominator;
                int denominator = num2.Denominator;
                num2.Denominator = nominator;
                num2.Nominator   = denominator;
            }

            resultFraction.Nominator   = num1.Nominator * num2.Nominator;
            resultFraction.Denominator = num1.Denominator * num2.Denominator;

            // attempting to shorten the result fraction
            resultFraction = Utils.ShortenFraction(resultFraction);

            return(resultFraction);
        }
示例#23
0
 //  FUnctional Developement - универсален калкулатор, който може да приема различни функции като вход. парам.
 public double Calculate(MathAction action, double num)
 {
     return(action(num));
 }
示例#24
0
        static void Main()
        {
            // Instantiate delegate with named method:
            MathAction ma = Double;

            // Invoke delegate ma:
            double multByTwo = ma(4.5);

            Console.WriteLine("multByTwo: {0}", multByTwo);

            // Instantiate delegate with anonymous method:
            MathAction ma2 = delegate(double input)
            {
                return(input * input);
            };

            double square = ma2(5);

            Console.WriteLine("square: {0}", square);

            // Instantiate delegate with lambda expression
            MathAction ma3  = s => s * s * s;
            double     cube = ma3(4.375);

            Console.WriteLine("cube: {0}", cube);

            Console.WriteLine((MyEnum)2);
            Console.ReadKey(true);

            //for (int i = 0; i < 20; i++)
            //{
            //    Console.WriteLine("INSERT INTO [dbo].[SESSION_LO]([URL],[NOM]) VALUES('" + Randomizer.Chaine(3) + "','" + Randomizer.Chaine(2) + "')");

            //}
            //Console.ReadKey(true);

            //for (int i = 0; i < 0; i++)
            //{
            //    Console.WriteLine("INSERT [dbo].[DATA_TRAITEMENT] ([LECTEUR_ID], [DATE], [NUM_CYCLE], [IDBLO], [IDEL], [IS_ENDOSSE], [IS_COMPTABILISE], [BAC_ID], [CONFIG_BULLETIN_LO_ID], [SESSION_LO_ID], [TYPE_TRAITEMENT_ID], [ETAT_LECTURE]) VALUES (" +
            //        Randomizer.Nombre(1, 1).ToString() + ", CAST(N'2018-04-20T16:04:29." +
            //        i.ToString("000") + "' AS DateTime), " +
            //        Randomizer.Nombre(20000).ToString() + ", N'" +
            //        Randomizer.Nombre(10000).ToString() + "', " +
            //        Randomizer.Nombre(500).ToString() + ", " +
            //        Randomizer.Nombre(3000).ToString() + ", " +
            //        Randomizer.Nombre(0, 2).ToString() + ", " +
            //        Randomizer.Nombre(1, 7).ToString() + ", " +
            //        Randomizer.Nombre(1, 3).ToString() + ", " +
            //        Randomizer.Nombre(1, 3).ToString() + ", " +
            //        Randomizer.Nombre(1, 3).ToString() + ", " +
            //        Randomizer.Nombre(1, 3).ToString() + ")");

            //}
            //Console.ReadKey(true);

            //for (int i = 0; i < 20; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO[dbo].[RESULTAT_VALEUR]([NB_EXPRESSIONS],[NB_SUFFRAGES]) VALUES(" + Randomizer.Nombre(20000).ToString() + "," + Randomizer.Nombre(20000).ToString() + ")"
            //        );

            //}
            //for (int i = 0; i < 200; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO [dbo].[RESULTATS_LO]([ID]) VALUES (" + i.ToString() + ")");
            //}

            //for (int i = 0; i < 8; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO [dbo].[RESULTAT_CONFIG_BULLETIN_LO] ([ID],[RESULTATS_LO_ID]) VALUES (" + i.ToString() + "," + Randomizer.Nombre(200).ToString() + ")");
            //}

            //for (int i = 0; i < 20; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO [dbo].[RESULTAT_VALEUR_BLANCHE]([ID])VALUES(" + i.ToString() + ")"
            //        );
            //}

            //for (int i = 0; i < 20; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO [dbo].[RESULTAT_VALEUR_Nulle]([ID])VALUES(" + i.ToString() + ")"
            //        );
            //}

            //for (int i = 0; i < 20; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO [dbo].[RESULTAT_VALEUR_VALABLEMENT_EXPRIMEE]([ID])VALUES(" + i.ToString() + ")"
            //        );

            //}

            //       for (int i = 6; i < 20; i++)
            //       {
            //           Console.WriteLine(
            //               "INSERT INTO [dbo].[RESULTAT_ZONE_EXPRESSION_VOTE]([ID],[VALEUR_BLANCHE_ID],[VALEUR_NULLE_ID],[VALEUR_VALABLEMENT_EXPRIMEE_ID],[RESULTAT_CONFIG_BULLETIN_LO_ID])" +
            //"VALUES(" + i.ToString() + "," + Randomizer.Nombre(20).ToString() + "," +
            //Randomizer.Nombre(20).ToString() + "," +
            //Randomizer.Nombre(20).ToString() + "," + Randomizer.Nombre(7).ToString() + ")");

            //       }

            //       for (int i = 6; i < 20; i++)
            //       {
            //           Console.WriteLine(
            //               "INSERT INTO [dbo].[RESULTAT_ZONE_EXPRESSION_VOTE]([ID],[VALEUR_BLANCHE_ID],[VALEUR_NULLE_ID],[VALEUR_VALABLEMENT_EXPRIMEE_ID],[RESULTAT_CONFIG_BULLETIN_LO_ID])" +
            //"VALUES(" + i.ToString() + "," + i.ToString() + "," +
            // i.ToString() + "," + i.ToString() + "," + i.ToString() + ")");

            //       }

            //for (int i = 0; i < 20; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO [dbo].[RESULTAT_ZONE_EXPRESSION_CAB]([ID])VALUES(" + Randomizer.Nombre(20).ToString() + ")"
            //        );

            //}

            //for (int i = 0; i < 20; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO [dbo].[RESULTAT_ZONE_EXPRESSION_CAC]([ID])VALUES(" + Randomizer.Nombre(20).ToString() + ")"
            //        );

            //}



            //for (int i = 0; i < 20; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO [dbo].[RESULTATS_LO]([ID])VALUES(" + Randomizer.Nombre(200).ToString() + ")"
            //        );

            //}

            //for (int i = 0; i < 100; i++)
            //{
            //    Console.WriteLine(
            //        "INSERT INTO [dbo].[RESULTAT_CONFIG_BULLETIN_LO]([ID],[RESULTATS_LO_ID])VALUES("+i.ToString()+","+
            //        Randomizer.Nombre(20).ToString() + ")");
            //}

            //for (int i = 0; i < 100; i++)
            //{
            //    Console.WriteLine(
            //       "INSERT INTO [dbo].[CONFIG_BULLETIN_LO]([ID],[LIBELLE],[STATION_LECTURE_CAC_CONTROLE_ID],[REGLE_REMPLISSAGE],[CONFIG_LO_ID]) VALUES("+ i.ToString()+", '"+Randomizer.Chaine(2)+ "', " + Randomizer.Nombre(8) + ", " + Randomizer.Nombre(200) + ", " + Randomizer.Nombre(1,3) + ")"
            //       );
            //}

            //for (int i = 0; i < 100; i++)
            //{
            //    Console.WriteLine(
            //       "insert into dbo.RESULTAT_VALEUR_POSSIBLE (ID, ID_COMMUN, RESULTAT_ZONE_EXPRESSION_VOTE_ID) values (" + i.ToString() + "," + Randomizer.Nombre(8) + ", " + Randomizer.Nombre(8) + ")"

            //       /*+ i.ToString() + ", '" + Randomizer.Chaine(2) + "', " + Randomizer.Nombre(8) + ", " + Randomizer.Nombre(200) + ", " + Randomizer.Nombre(1, 3) + ")"*/
            //       );
            //}

            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(
                    "insert into dbo.CAC_POSSIBLE(ID, LIBELLE, COORDONNEE_X, COORDONNEE_Y, ZONE_EXPRESSION_CAC_ID) values(" + i.ToString() + ",'" + Randomizer.Chaine(1) + "', 7,7," + Randomizer.Nombre(1, 8) + ")");
            }

            //DoZones();

            Console.ReadKey(true);
        }
示例#25
0
 public UsingDelegate(MathAction action)
 {
     this.action = action;
 }
示例#26
0
    void Start()
    {
        var stopwatch = new Stopwatch();

        //var testClassInstance = new TestClass();
        //    bool bool1 = true;

        //System.Type type = typeof(bool);
        //System.Type type2 = typeof(string);

        var speed = stopwatch.RunTest(() =>
        {
            for (long i = 0; i < NumIterations; ++i)
            {
                Nested.DirectMethod();
            }
        });

        // print("Direct Method: " + speed);
        MathAction ma = Nested.DirectMethod;

        speed = stopwatch.RunTest(() =>
        {
            for (long i = 0; i < NumIterations; ++i)
            {
                bool1 = ma();
            }
        });
        print("Delegated Method: " + speed);
        speed = stopwatch.RunTest(() =>
        {
            for (long i = 0; i < NumIterations; ++i)
            {
                bool1 = DirectMethod();
            }
        });
        print("Direct Method: " + speed);
        speed = stopwatch.RunTest(() =>
        {
            for (long i = 0; i < NumIterations; ++i)
            {
                bool1 = Nested.DirectMethod();
            }
        });
        print("Nested Direct Method: " + speed);
        speed = stopwatch.RunTest(() =>
        {
            for (long i = 0; i < NumIterations; ++i)
            {
                bool1 = Nested.bool1;
            }
        });
        print("Nested Direct field verification: " + speed);

        speed = stopwatch.RunTest(() =>
        {
            for (long i = 0; i < NumIterations; ++i)
            {
                bool1 = Nested.bool1;
            }
        });
        print("Nested Direct field verification: " + speed);

        //var nestedVariableClassAcess = stopwatch.RunTest(() =>
        //{
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }
        //    if (testClassInstance.field == true) { }

        //});
        //var Nested3Value = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        Nested._nested2._nested3.value += 1f;
        //    }
        //});

        //var Nested0Value = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        value += 1f;
        //    }
        //});

        //var Nested1Value = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        Nested.value += 1f;
        //    }
        //});

        //var Nested2Value = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        Nested._nested2.value += 1f;
        //    }
        //});

        //var emptyFor = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //    }
        //});

        //var CompareType = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        if (type == type2)
        //        {
        //        }
        //    }
        //});

        //var CompareBool = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < 0; ++i)
        //    {
        //        if (bool1 == bool2) { }
        //    }
        //});

        //var CommpareString = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < 0; ++i)
        //    {
        //        if (string1 == string2) { }
        //    }
        //});

        //var CompareSplit = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < 0; ++i)
        //    {
        //        string[] splitedString = stringToSplit.Split('.');
        //    }
        //});

        //var getTypeTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < 0; ++i)
        //    {
        //        // testClassType = typeof(TestClass);
        //    }
        //});

        //var getFieldTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < 0; ++i)
        //    {
        //        // intFieldInfo = testClassType.GetField("IntField");
        //    }
        //});

        //var getPropertyTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < 0; ++i)
        //    {
        //        // intPropertyInfo = testClassType.GetProperty("IntProperty");
        //    }
        //});

        //var getMethodTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < 0; ++i)
        //    {
        //        // voidMethodInfo = testClassType.GetMethod("VoidMethod");
        //    }
        //});

        //var readFieldReflectionTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        // intValue = (int)intFieldInfo.GetValue(testClassInstance);
        //    }
        //});

        //var readPropertyReflectionTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        //  intValue = (int)intPropertyInfo.GetValue(testClassInstance, null);
        //    }
        //});

        //var readFieldDirectTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < 0; ++i)
        //    {
        //        //   intValue = testClassInstance.IntField;
        //    }
        //});

        //var readPropertyDirectTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        //  intValue = testClassInstance.IntProperty;
        //    }
        //});

        //var writeFieldReflectionTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        // intFieldInfo.SetValue(testClassInstance, 5);
        //    }
        //});

        //var writePropertyReflectionTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        // intPropertyInfo.SetValue(testClassInstance, 5, null);
        //    }
        //});

        //var writeFieldDirectTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        //testClassInstance.IntField = intValue;
        //    }
        //});

        //var writePropertyDirectTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        //testClassInstance.IntProperty = intValue;
        //    }
        //});

        //var callMethodReflectionTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        //voidMethodInfo.Invoke(testClassInstance, null);
        //    }
        //});

        //var callMethodDirectTime = stopwatch.RunTest(() =>
        //{
        //    for (long i = 0; i < NumIterations; ++i)
        //    {
        //        // testClassInstance.VoidMethod();
        //    }
        //});

        //report = "Test,Reflection Time,Direct Time\n"
        //            + "Nested0Value," + Nested0Value + ",0\n"
        //                        + "Nested1Value," + Nested1Value + ",0\n"
        //                                    + "Nested2Value," + Nested2Value + ",0\n"
        //                                                + "Nested3Value," + Nested3Value + ",0\n"
        //    + "rootVariableAcess," + rootVariableAcess + ",0\n"
        //    + "nestedVariableClassAcess," + nestedVariableClassAcess + ",0\n"
        //    + "CompareSplit," + CompareSplit + ",0\n"
        //    + "emptyFor," + emptyFor + ",0\n"
        //    + "CompareType," + CompareType + ",0\n"
        //    + "CompareBool," + CompareBool + ",0\n"
        //    + "CommpareString," + CommpareString + ",0\n"
        //    + "Get Type," + getTypeTime + ",0\n"
        //    + "Get Field," + getFieldTime + ",0\n"
        //    + "Get Property," + getPropertyTime + ",0\n"
        //    + "Get Method," + getMethodTime + ",0\n"
        //    + "Read Field," + readFieldReflectionTime + "," + readFieldDirectTime + "\n"
        //    + "Read Property," + readPropertyReflectionTime + "," + readPropertyDirectTime + "\n"
        //    + "Write Field," + writeFieldReflectionTime + "," + writeFieldDirectTime + "\n"
        //    + "Write Property," + writePropertyReflectionTime + "," + writePropertyDirectTime + "\n"
        //    + "Call Method," + callMethodReflectionTime + "," + callMethodDirectTime + "\n";
    }
示例#27
0
        public static MyFraction AddSubstractHandler(MyFraction num1, MyFraction num2, MathAction action)
        {
            // preparing output variable
            MyFraction resultFraction = new MyFraction();

            // creating variables which we can change later in the if statement
            int newNominator1, newNominator2, newNominator, newDenominator;

            // based on equality or unequality of denominators, their further calculations are different
            if (num1.Denominator != num2.Denominator)
            {
                newNominator1 = num1.Nominator * num2.Denominator;
                newNominator2 = num2.Nominator * num1.Denominator;
                // the only difference between addition and substration is handled here by a ternary operator
                newNominator               = action == MathAction.ADD ? newNominator1 + newNominator2 : newNominator1 - newNominator2;
                newDenominator             = num1.Denominator * num2.Denominator;
                resultFraction.Nominator   = newNominator;
                resultFraction.Denominator = newDenominator;
            }
            else
            {
                // ternary operator again
                newNominator               = action == MathAction.ADD ? num1.Nominator + num2.Nominator : num1.Nominator - num2.Nominator;
                resultFraction.Nominator   = newNominator;
                resultFraction.Denominator = num1.Denominator;
            }

            return(resultFraction);
        }