static void Main()
		{
			MyClass mc = new MyClass();
			MyDel myDel = mc.Add2;	// Create and initialize the delegate.

			myDel += mc.Add3;	// Add a method.
			myDel += mc.Add2;	// Add a method.
			Console.WriteLine("Value: {0}", myDel()); // Invoke the delegate and use the return value.
		}
Esempio n. 2
0
        static void Main()
        {
            MyClass instance = new MyClass();

            MyDelegate myDelegate = new MyDelegate(instance.Method); // Создаем экземпляр делегата. (2)

            myDelegate.Invoke();   // Вызываем метод сообщенный с делегатом. (3)

            myDelegate();          // Другой способ вызова метода сообщенного с делегатом. (3')

            // Delay.
            Console.ReadKey();
        }
		static void Main()
		{
			MyClass mc = new MyClass();
			MyDel myDel = mc.Add2;

			myDel += mc.Add3;
			myDel += mc.Add2;

			int x = 5;
			myDel(ref x);

			Console.WriteLine("Value: {0}", x);
		}
Esempio n. 4
0
        static void Main()
        {
            var instance = new MyClass();

            // can refer an instance method
            MyDelegate stringInspector = instance.CountCharacters;
            // apelat ca o funcție; are un singură metodă
            stringInspector("Vasile");

            // can refer static methods
            stringInspector += MyClass.PrintBeginning;
            // apelat cu Invoke; are două funcții
            stringInspector.Invoke("Cozonac");

            stringInspector -= instance.CountCharacters;
            // apelat asincron, just to mess with you
            stringInspector.BeginInvoke(
                "Paranoia",
                ar =>
                {
                    stringInspector.EndInvoke(ar);
                    Console.WriteLine("Am încheiat apelul asincron");
                }, null);

            Console.ReadKey(true);

            // can create anonymous delegates
            stringInspector += delegate (string s)
            {
                Console.WriteLine("„{0}” has {1} characters", s, s.Length);
            };

            stringInspector("masonerie");

            // delegați pre-definiți
            // are un parametru int
            Action<int> action1;
            // are un parametru int, unul string
            Action<int, string> action2;

            // returnează un întreg
            Func<int> func1;
            // are un parametru string, returnează un int
            Func<string, int> func2;
            // un fel de Func<T, bool>
            Predicate<Uri> uriValidator;
        }
Esempio n. 5
0
        static void Main()
        {
            MyClass instance = new MyClass();

            MyDelegate myDelegate = new MyDelegate(instance.Method); // Создаем экземпляр делегата и сообщаем с ним метод. (2)

            string greeting = myDelegate.Invoke("Jeffrey Richter"); // Вызываем метод сообщенный с делегатом. (3)

            Console.WriteLine(greeting);

            greeting = myDelegate("Grady Booch"); // Другой способ вызова метода сообщенного с делегатом. (3')

            Console.WriteLine(greeting);

            // Delay.
            Console.ReadKey();
        }
Esempio n. 6
0
		static void Main(string[] args)
		{
			// Class methods as delegates
			Delegates.MyDelegate f;

			f = Delegates.Func1;
			Console.WriteLine("The number is: " + f(10, 20));

			f = Delegates.Func2;
			Console.WriteLine("The number is: " + f(10, 20));

			// Object methods as delegates
			MyClass mc = new MyClass();

			f = mc.InstanceMethod1;
			Console.WriteLine("The number is: " + f(10, 20));

			// Anonymous delegates - e.g. delegate implementation is done inline.
			f = delegate(int arg1, int arg2) { return (arg1/arg2).ToString(); };
			Console.WriteLine("The number is: " + f(20, 20));

			// Composable delegates - e.g. chaining delegate methods into one variable.
			Delegates.MyDelegate f1 = Delegates.Func1;
			Delegates.MyDelegate f2 = Delegates.Func2;
			Delegates.MyDelegate composedDelegates = f1 + f2;

			Console.WriteLine("The number of the delegate chain: " + composedDelegates(5, 5));

			composedDelegates -= f2;

			Console.WriteLine("The number of the delegate chain: " + composedDelegates(5, 5));

			// Composable delegates w. references - e.g.

			Delegates.MyRefDelegate ref1 = Delegates.RefFunc1;
			Delegates.MyRefDelegate ref2 = Delegates.RefFunc2;
			Delegates.MyRefDelegate combinedRefDels = ref1 + ref2;

			int a = 10, b = 10;

			combinedRefDels(a, ref b);

			// End of theory
			Console.WriteLine("==================END OF DELEGATE THEORY==================");


			// Start of Exercise
			Console.WriteLine("==================START OF DELEGATE EXERCISE==================");

			// Variables
			ShippingFeesDelegate theDel;
			ShippingDestination theDest;
			string theZone;

			do
			{
				// Get the destination zone
				Console.WriteLine("What is the destination zone?");
				theZone = Console.ReadLine();
				// Terminate the program if the user writes "exit"
				if (!theZone.Equals("exit"))
				{
					// Given the zone, retrieve the associated shipping fee
					theDest = ShippingDestination.GetShippingDestinationInfo(theZone);

					// If there's no associated info, then the user entered an invalide zone, otherwise continue
					if (theDest != null)
					{
						// Ask for price and convert the string to a decimal number
						Console.WriteLine("What is the item price?");
						string thePriceAsString = Console.ReadLine();
						decimal itemPrice;
						Decimal.TryParse(thePriceAsString, out itemPrice);

						// Using calcFees delegate to calculate fee for item.
						theDel = theDest.calcFees;

						if (theDest._isHighRisk)
						{
							theDel += delegate(decimal thePrice, ref decimal itemFee) { itemFee += 25.0m; };
						}

						// Call delegate to calcualte shipping fee + write output
						decimal theFee = 0.0m;
						theDel(itemPrice, ref theFee);
						Console.WriteLine($"The shipping fees are: {theFee}");
					}
					else
					{
						Console.WriteLine("Unknown destionation. Available zones are zone1 to zone4. Write exit to exit.");
					}
				}
			} while (theZone != "exit");

			// Start of Exercise
			Console.WriteLine("==================END OF DELEGATE EXERCISE==================");
		}