Untitled

 avatar
unknown
plain_text
10 months ago
3.7 kB
19
Indexable
using System;

// Define the Calculation delegate 
public delegate void Calculation(decimal val1, decimal val2, ref decimal result);

// Declare the namespace
namespace DelegateExample
{
        // Define the DelegateExample class
            public class DelegateExample
                {
                            // Define Mycalc1 and Mycalc2 as instances of the Calculation delegate
                                    public Calculation Mycalc1;
                                            public Calculation Mycalc2;

                                                    // Define the Add method for addition
                                                            public static void Add(decimal add1, decimal add2, ref decimal result)
                                                                    {
                                                                                    result = add1 + add2;
                                                                                                Console.WriteLine("add {0} + {1} = {2}", add1, add2, result);
                                                                    }

                                                                            // Define the Sub method for subtraction
                                                                                    public static void Sub(decimal sub1, decimal sub2, ref decimal result)
                                                                                            {
                                                                                                            result = sub1 - sub2; 
                                                                                                                        Console.WriteLine("sub {0} - {1} = {2}", sub1, sub2, result);
                                                                                            }
                }

                    // Main program
                        class Program
                            {
                                        static void Main(string[] args)
                                                {
                                                                decimal result = 0.0m;
                                                                            DelegateExample del = new DelegateExample();

                                                                                        // Instantiate Mycalc1 and Mycalc2 with the Add and Sub methods
                                                                                                    del.Mycalc1 = new Calculation(DelegateExample.Add);
                                                                                                                del.Mycalc2 = new Calculation(DelegateExample.Sub);

                                                                                                                            // Invoke the delegates
                                                                                                                                        del.Mycalc1(10.5m, 5.2m, ref result);
                                                                                                                                                    del.Mycalc2(10.5m, 5.2m, ref result);
                                                }
                            }
}
                                                }
                            }
                                                                                            }
                                                                    }
                }
}
Editor is loading...
Leave a Comment