static void Main() { Rectangle rect = new Rectangle(); RectDelegate obj = new RectDelegate(rect.GetArea); obj += rect.GetPerimeter; obj.Invoke(56.23, 23.45); Console.WriteLine("\n"); //Break line Console.WriteLine("Good Morning"); Traingle trai = new Traingle(); traiDelegate obj1 = new traiDelegate(trai.TraiArea); obj1.Invoke(56.36, 58.56); Console.WriteLine("\n"); Console.WriteLine("Good Morning"); TestdelegateMethod p = new TestdelegateMethod(); DelMethod del = new DelMethod(p.method_1); // Here we have multicast del += new DelMethod(p.method_2); Console.WriteLine(del(50, 10)); // Here again we have multicast del -= new DelMethod(p.method_2); Console.WriteLine(del(20, 10)); Console.ReadKey(); }
public static void Run() { Console.WriteLine("Enter the Delegate value:"); int b = int.Parse(Console.ReadLine()); DelMethod delobj = new DelMethod(DelFunction); delobj(b); Console.ReadLine(); }
public static void Main() { TestDelegate td = new TestDelegate(); DelMethod AddDelegate = new DelMethod(td.Add); DelMethod SubDelegate = new DelMethod(td.Subtract); DelMethod MulDelegate = new DelMethod(td.Multiply); int addAns = AddDelegate(10, 15); int subAns = SubDelegate(40, 20); int mulAns = MulDelegate(2, 5); Console.WriteLine("Addition: {0}\nSubtration: {1}\nMultiplication: {}", addAns, subAns, mulAns); }
static void Main(string[] args) { // A delegate is a type safe function pointer DelMethod del = new DelMethod(Methoddel); del("Hello Humphry"); Method2del meth2 = new Method2del(Method2); string companyname = meth2.Invoke("Shiktech"); Console.WriteLine(companyname); Callbacks callbacks = new Callbacks(); callbacks.Longrunningloop(Callbackmethod); }
public Calculator(string expression) { this.tree = CSharpSyntaxTree.ParseText(@"using System; namespace Calc { class Pars { public double ret(double x, double y) { return " + expression + @"; } } } "); var compilation = CSharpCompilation.Create("pars", new[] { tree }, new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) }, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); var mem = new MemoryStream(); EmitResult result = compilation.Emit(mem); if (!result.Success) { foreach (Diagnostic item in result.Diagnostics) { Console.Error.WriteLine("{0}: {1}", item.Id, item.GetMessage()); } return; } mem.Seek(0, SeekOrigin.Begin); Assembly assembly = Assembly.Load(mem.ToArray()); Type type = assembly.GetType("Calc.Pars"); ConstructorInfo constInfo = type.GetConstructors().First(); var instance = constInfo.Invoke(null); MethodInfo method = type.GetMethod("ret"); delegat = (DelMethod)method.CreateDelegate(typeof(DelMethod), instance); }
static void Main(string[] args) { MathOperations mathOperations = new MathOperations(); DelMethod delmethod = new DelMethod(mathOperations.Sum); delmethod += new DelMethod(mathOperations.Substract); //Substract method add the chain ForegroundColor = DarkGray; WriteLine("***** Call the Sum and Substract methods from the chain ******"); delmethod(20, 30); ///call the Sum and Substract methods delmethod += new DelMethod(mathOperations.Division); //Division method add the chain delmethod += new DelMethod(mathOperations.Multiple); //Multiple method add the chain ForegroundColor = DarkMagenta; WriteLine("***** Call the all methods from the chain ******"); delmethod(20, 30); ///call the Sum, Substract, Multiple and Division methods //remove the Multiple method from the chain ForegroundColor = DarkRed; WriteLine("***** remove the Multiple method from the chain ******"); delmethod -= new DelMethod(mathOperations.Division); //Division method add the chain delmethod(20, 30); ///call the Sum, Substract and Division methods ReadKey(); }