Introduction
C# Anonymous methods provide additional, subtraction, multiplication, and division while parsing the body value. It is available with static and dynamic based value parsing.
- num += a;
-
- Console.WriteLine("Named addition: {0}", num);
Subtraction:
- num -= s;
-
- Console.WriteLine("Named Subtraction: {0}", num);
Multiplication:
- num *= m;
-
- Console.WriteLine("Named Multiplication: {0}", num);
Division:
- num /= d;
-
- Console.WriteLine("Named Division: {0}", num);
The Delegate Function determines to refer to the method same signature of the delegate. The delegate method calls to the main method that the main method calls for additional, subtraction, multiplication, and division.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- delegate void AnonymousArithmetic(int n);
- namespace Anonymous
- {
- class TestDelegate
- {
- static int num = 20;
-
- public static void additional(int a)
- {
- num += a;
- Console.WriteLine("Named addition: {0}", num);
- }
- public static void subtraction(int s)
- {
- num -= s;
- Console.WriteLine("Named subtraction: {0}", num);
- }
- public static void multiplication(int m)
- {
- num *= m;
- Console.WriteLine("Named multiplication: {0}", num);
- }
- public static void division(int d)
- {
- num /= d;
- Console.WriteLine("Named division: {0}", num);
- }
-
- public static int getNum()
- {
- return num;
- }
-
- static void Main(string[] args)
- {
-
- AnonymousArithmetic Anonymousvalue = delegate (int x) {
- Console.WriteLine("Anonymous Method: {0}", x);
- };
-
-
- Anonymousvalue(20);
-
-
- Anonymousvalue = new AnonymousArithmetic(additional);
- Anonymousvalue(10);
-
-
- Anonymousvalue = new AnonymousArithmetic(subtraction);
- Anonymousvalue(10);
-
-
- Anonymousvalue = new AnonymousArithmetic(multiplication);
- Anonymousvalue(10);
-
-
- Anonymousvalue = new AnonymousArithmetic(division);
- Anonymousvalue(2);
-
- Console.ReadKey();
- }
- }
- }
Sample Static Output:
This functionality to be declared is static-based. The same method also can be implemented on Dynamic-based value parsing.