예제 #1
0
 public static void ForAll(SquareDelegate squareDelegate)
 {
     foreach (Square square in All)
     {
         squareDelegate(square);
     };
 }
예제 #2
0
 public static void ForAll(SquareDelegate squareDelegate)
 {
     foreach (Square square in All)
     {
         squareDelegate(square);
     }
     ;
 }
예제 #3
0
        public static void TestTopic()
        {
            SquareDelegate squareDelegate = GetRectangleSidesSum;

            Square baseSquare = CovarianceDelegate.BuildBaseSquare();

            int rectangleSidesSum = squareDelegate(baseSquare);

            Debug.WriteLine($"Contravariance: Rectangle sides sum is {rectangleSidesSum}.");
        }
    internal void CreateDynamicMethod()
    {
        MethodInfo    getSquare       = typeof(Square).GetMethod("CalculateSquare");
        DynamicMethod calculateSquare = new DynamicMethod("CalculateSquare",
                                                          typeof(int), new Type[] { typeof(int) });
        ILGenerator il = calculateSquare.GetILGenerator();

        // Load the first argument, which is a integer, onto the stack.
        il.Emit(OpCodes.Ldarg_0);
        // Call the overload of CalculateSquare that returns the square of number
        il.Emit(OpCodes.Call, getSquare);
        il.Emit(OpCodes.Ret);
        SquareDelegate hi =
            ( SquareDelegate )calculateSquare.CreateDelegate(typeof(SquareDelegate));

        Console.WriteLine("\r\nUse the delegate to execute the dynamic method:");
        int retval = hi(42);

        Console.WriteLine("Calculate square returned " + retval);
    }
예제 #5
0
        static void Main(string[] args)
        {
            Console.Write("Number to square: ");
            int.TryParse(Console.ReadLine(), out int value);
            SquareDelegate square = Square;

            Console.WriteLine($"Square is: {square.Invoke(value)}");
            var collectionCheck = new CollectionCheck();

            collectionCheck.AddItem("A");
            collectionCheck.AddItem("B");
            collectionCheck.AddItem("C");
            collectionCheck.AddItem("D");
            collectionCheck.AddItem("E");
            var items = collectionCheck.Items;

            items.ToList();
            Console.WriteLine($"Const are static: {CollectionCheck.CONST_ARE_STATIC}");
            Console.ReadLine();
        }
예제 #6
0
파일: MainClass.cs 프로젝트: Grukz/Sample
    delegate int SquareDelegate(int x);               // xの平方を求める関数を持つデリゲート.

    // メインメソッド
    static void Main()  // Mainメソッドの定義
    {
        // ファイルチェックデリゲートの生成.(処理内容をラムダ式で定義.)
        FileCheckDelegate fcd = (filename) =>
        {
            // ファイルが存在するかをチェック.
            if (File.Exists(filename))  // File.Existsでfilenameなるファイルが存在するかチェック.
            {
                // 存在する場合.
                Console.WriteLine(filename + " is Exists."); // "Exists"と出力.
                return(true);                                // trueを返す.
            }
            else
            {
                // 存在しない場合.
                Console.WriteLine(filename + " is not Exists."); // "not Exists"と出力.
                return(false);                                   // falseを返す.
            }
        };

        // "test.txt"があるかどうかチェック.
        if (fcd("test.txt"))    // fcdに"test.txt"を渡す.
        {
            // OK
            Console.WriteLine("Check OK."); // "Check OK."と出力.
        }
        else
        {
            // NG
            Console.WriteLine("Check NG."); // "Check NG."と出力.
        }

        // スクウェアデリゲートの生成.(処理内容をラムダ式で定義.)
        SquareDelegate sd = x => x * x; // 単文だとこれだけでいい.

        // 5の平方を求める.
        Console.WriteLine("sd(5) = " + sd(5));  // 5の平方の値を出力.
    }
예제 #7
0
        //public delegate void PrintDelegateWithParams(string str);
        //public void Print()
        //{
        //    Console.WriteLine("Msg from print function");
        //}
        //public void PrintParameters(string s)
        //{
        //    Console.WriteLine(s);
        //}
        static void Main(string[] args)
        {
            Program         p  = new Program();
            DisplayDelegate d2 = delegate(string n1, int n2)
            {
                Console.WriteLine(n1);
                Console.WriteLine(n2);
            };
            PrintDelegate d1 = delegate()
            {
                Console.WriteLine("Msg from anonymous function");
            };

            d1();
            d2("I am everywhere", 10);
            SquareDelegate d3 = (x => x * x);

            Console.WriteLine(d3(45));
            //PrintDelegate d1 = new PrintDelegate(p.Print);
            //PrintDelegateWithParams d2 = new PrintDelegateWithParams(p.PrintParameters);
            //d1();
            //d2("Message from print parameters");
        }