//This is the method that is going to be passed to the delegate /* public static string Greetings(string name) * { * return "Hello "+ name + " a very good morning!"; * }*/ public static void Main() { /*//Normal way of using delegates * //Here we are actually passing the method which we created earlier * // GreetingsDelegate del = new GreetingsDelegate(Greetings);*/ //Anonymous Method //Here there is no need to create a method to write the logic but we can just use the delegate keyword and create an instance to call it GreetingsDelegate del = delegate(string name) { return("Hello " + name + " a very good morning!"); }; /* //Lambda Expressions * //Short hand for writing anonymous functions * GreetingsDelegate del = (name)=> * { * return "Hello " + name + " a very good morning!"; * };*/ string str = del.Invoke("CJAY"); Console.WriteLine(str); Console.ReadLine(); }
static void Main(string[] args) { //Different ways of decelaring a delegate GreetingsDelegate del1 = new GreetingsDelegate(Greetings); GreetingsDelegate del2 = Greetings; GreetingsDelegate del3 = delegate(string name) { return("Hello @" + name + " welcome to Dotnet Tutorials this is an Anonymous Function"); }; GreetingsDelegate del4 = (theName) => { return("Hello @" + theName + " welcome to DotNet Tutorials"); }; string GreetingMsg = del1.Invoke("Pranaya"); string GreetingMsg2 = del2("Pranaya"); string GreetingMsg3 = del3("Pranaya"); string GreetingMsg4 = del4.Invoke("This Name Lambda"); Console.WriteLine(GreetingMsg); Console.WriteLine(GreetingMsg2); Console.WriteLine(GreetingMsg3); Console.WriteLine(GreetingMsg4); }
internal void Hello() { GreetingsDelegate handler = SaySomething; handler("Hello World"); GreetingsDelegate handlerGreetings = new GreetingsDelegate(SaySomething); handlerGreetings("Huhu"); GreetingsDelegate greetingsDelegate = delegate(string val) { Console.WriteLine("Inside Anonymous method. Value: {0}", val); }; greetingsDelegate("Sample"); Action <string> greet = name => { string greeting = $"Hallo {name}!"; Console.WriteLine(greeting); }; greet("World"); }
static void Main(string[] args) { GreetingsDelegate obj = new GreetingsDelegate(Greetings); GreetingsDelegate obj5 = (name) => { return("Hello " + name + " a very good morning."); }; Func <int, float, double, double> obj1 = Generic1Delegates.AddNums1; double res = obj1.Invoke(100, 34.5f, 193.465); Console.WriteLine(res); Action <int, float, double> obj2 = Generic1Delegates.AddNums2; obj2.Invoke(100, 34.5f, 193.465); Predicate <string> obj3 = Generic1Delegates.checkLength; bool rr = obj3.Invoke("nitesh"); Console.WriteLine(rr); //string str = obj.Invoke("Nitesh"); //string str1 = obj5.Invoke("Nitesh"); //Console.WriteLine(str); //Console.WriteLine(str1); Console.ReadKey(); }
static void Main(string[] args) { MultiplynumsDelegate obj = delegate(int x, int y) { return(x * y); }; double result1 = obj.Invoke(10, 20);//New variable to store the value -return type Console.WriteLine(result1); QuickDelegate p = delegate(string name1) { return("Hello" + name1); }; string name = p.Invoke("Yelleti"); Console.WriteLine(name); Addnums2Delegate obj2 = delegate(int x, float y, double z) { Console.WriteLine(x + y + z); }; obj2.Invoke(10, 3.142f, 123456.7809738);//Non return type GreetingsDelegate w = delegate(string wish) { Console.WriteLine(wish + " " + "Happy Birthday!!!!!!"); }; w.Invoke("wish you "); Console.ReadLine(); }
static void Main(string[] args) { MultiplynumsDelegate obj = new MultiplynumsDelegate(Multiplynums); double result1 = obj.Invoke(10, 20);//New variable to store the value -return type Console.WriteLine(result1); QuickDelegate p = new QuickDelegate(Quick); string name = p.Invoke("Mumma"); Console.WriteLine(name); Addnums2Delegate obj2 = new Addnums2Delegate(Addnums2); obj2.Invoke(10, 3.142f, 123456.7809738);//Non return type GreetingsDelegate w = new GreetingsDelegate(Wishes); w.Invoke("wish you "); CheckLengthDelegate obj3 = new CheckLengthDelegate(CheckLength);//New variable to store the value -return type bool check = obj3.Invoke("Haritha"); Console.WriteLine(check); CheckLengthDelegate o = new CheckLengthDelegate(Value);//New variable to store the value -return type bool ch = o.Invoke("Maredapaaly"); Console.WriteLine(ch); Console.ReadLine(); }
static void Main() { //Normal Delegate GreetingsDelegate obj = new GreetingsDelegate(Greetings); string str = obj.Invoke("scott"); Console.WriteLine(str); //Anonymouse method GreetingsDelegate obj1 = delegate(string name) { return("hello " + name + " a very good morning!"); }; GreetingsDelegate obj2 = (name) => { return("Hello this is lambda expressin" + name + "ok"); }; string st = obj1("Naruto"); Console.WriteLine(st); }
static void Main(string[] args) { numsDelegate p = delegate(int a, int b) { Console.WriteLine(a + b); }; numsDelegate m = delegate(int c, int d) { Console.WriteLine(c * d); }; numsDelegate v = delegate(int e, int f) { Console.WriteLine(e + f); }; GreetingsDelegate g = delegate(string name) { Console.WriteLine("Hello Hi!!!!!!" + name); }; GreetingsDelegate w = delegate(string wish) { Console.WriteLine(wish + " " + "Happy Birthday!!!!!!"); }; p.Invoke(100, 50); m.Invoke(500, 600); v.Invoke(800, 9000); g.Invoke("Hari"); w.Invoke("wish you "); Console.ReadLine(); }
//01. Part 3 of delegates (Anonymonous Methods) //02. previous section we saw how to buind a method with delegate // we create instance of delegate and while creating the instance we pass the method as parameter to // that delegate (for binding) //03. (imp) ananymonous method is also related to delegate, with our binding a named method to a // delegate, we can bind an unnamed code block to the delegate //04. now we need to call the method using delegate (like how we have seen in part1 and part2 of delegates) //defined delegate //instanciate the delegate //invoke the delegate //////public static string Greetings(string name) { ////// return "Hello " + name + ", very good morning"; //////} public void MDelegateMain() { //07. create instance of the delegate here //////GreetingsDelegate objGreetingDelegate = new GreetingsDelegate(Greetings); //11. instead of that we can simplify the process // at the time of creating an instance of delegate, we can use the word as delegate followed by parameter type and name // followed by method body in open and close { } // which means there is no need to have the Greetings method sparately, // the Greetings method is created at the time of creating the instance of the delegate // the output is the same GreetingsDelegate objGreetingDelegate = delegate(string name2) { return("Hello " + name2 + ", very good morning"); }; //12. now we can remove the Greetings() method and run the program, we will get the same output //13. the below part is called anonymonous method, a method with out a name. what it contains is only a body //14. the method is defined using the delegate keyword /* * delegate (string name2) * { * return "Hello " + name2 + ", very good morning"; * }; */ //15. the advantage is less typing (no need to specify the modified, method name return type) // ananymonous methods are not often suggested //16. if the method is going to have less volumes of code, then it is recommedned to go with anonymonous method // for huge volumnes of code anonymonus methods are not recommeded //17. for a small logic with few lines of code we can write an unnamed code and directly bind it with the // anonymonous method //18. now the question is how does it understand the return type, the GreetingsDelegate() declared in line; #7 // already knows that the return type is string //19. there is no where to go and check the logic of the method, the logic is defined in the place where the // binding is created //08.now invoke the delegate (a) either using .invoke method or(b) call directly // the reference method accepts string as value and returns a string as output string hello = objGreetingDelegate("scott"); Console.WriteLine(hello); //09. lets run the program, the method is invoked with the help of delegate //OP: // Hello scott!!!, very good morning //10. this is what we have learned in the previous sessions with the help of a delegate, how to invoke a method // on a high level what we have done is, // (a) we have writted a method // (b) binding the method with delegate }
public void Show() { var a = "a"; var b = "b"; //通过new来建立委托的变量 DeletegateTest test = new DeletegateTest((string x, string y) => { Console.WriteLine(x); Console.WriteLine(y); }); test(a, b); //使用逆名方法的委托实现委托实例 DeletegateTest test2 = delegate(string x, string y) { Console.WriteLine(x); Console.WriteLine(y); }; test2(a, b); //使用Lambda Expressions实现委托实例 DeletegateTest test3 = (string x, string y) => { Console.WriteLine(x); Console.WriteLine(y); }; test3(a, b); //使用方法的注册来实例化委托 DeletegateStep1 stepAction = new DeletegateStep1(); GreetingsDelegate greetingDelegate = new GreetingsDelegate(stepAction.Greetings); GreetingsDelegate greetingDelegate2 = stepAction.Greetings; string result = greetingDelegate("welcome,tester"); Console.WriteLine(result); result = greetingDelegate2("jack"); Console.WriteLine(result); //string result1 = greetingDelegate.Invoke("welcome,tester"); IAsyncResult asyncResult = greetingDelegate.BeginInvoke("welcome,tester", null, null); //调用EndInvoke方法获取异步执行的结果 var invokeResult = greetingDelegate.EndInvoke(asyncResult); //BeginInvoke虽然是异步,但是仍然阻塞主线程 Console.WriteLine(invokeResult); //通过回调函数来避免阻塞,而将整个结果的返回写在AsyncCallBack里面 //这种可以达到异步的效果,不阻塞主线程 greetingDelegate.BeginInvoke("welcome,airven", ExcuteCompleted, null); Console.WriteLine("end"); Console.Read(); }
static void Main(string[] args) { GreetingsDelegate obj = new GreetingsDelegate(Greetings); //object ceation of delegate or INSTNTIATING A Delegate //Method is passing returning a value string str = obj.Invoke("girls"); Console.WriteLine(str); Console.ReadLine(); }
private void btnAnoniem_Click(object sender, RoutedEventArgs e) { // Anonieme methode die toegevoegd wordt aan de delegate GreetingsDelegate gd = delegate(string s) { MessageBox.Show(s, "Boodschap aan de wereld"); }; gd("Hello Word !"); }
static void Main(string[] args) { GreetingsDelegate gdel = delegate(string name) { return("Hello @" + name + "Welcome to Dotnet Tutorials"); }; string GreetingsMessage = gdel.Invoke("Pranaya"); Console.WriteLine(GreetingsMessage); }
static void Main(string[] args) { GreetingsDelegate obj = name => { return(" hello " + name + " good morning "); }; string str = obj.Invoke(" dad "); Console.WriteLine(str); Console.Read(); }
static void Main(string[] args) { GreetingsDelegate obj1 = delegate(string name) { return("hello" + name + "good morning"); }; string str = obj1.Invoke("swathi"); Console.WriteLine(str); Console.Read(); }
static void Main() { GreetingsDelegate obj = (name) => //this is Lambda expression , We can write (string name) but its not mandetory { return("Hello " + name + ", A very Good morning"); }; string str = obj.Invoke("Karren"); Console.WriteLine(str); Console.ReadLine(); }
//public static string Greetings(string Name) //{ // return "hello " + Name + " A very Good Morning"; //} static void Main() { //GreetingsDelegate obj = new GreetingsDelegate(Greetings); GreetingsDelegate obj = delegate(string name)//anonymous delegate { return("hello " + name + " A very Good Morning"); }; string str = obj.Invoke("scott"); Console.WriteLine(str); Console.ReadLine(); }
static void Main(string[] args) { GreetingsDelegate obj = (Name) => { return("hello " + Name + " a very Good Morning "); }; string str = obj.Invoke("sajan"); Console.WriteLine(str); Console.ReadLine(); }
//public static string Greetings(string name) //{ // return "Hello " + name + " A very Good Morning"; //} static void Main(string[] args) { GreetingsDelegate obj = delegate(string name) // Anonymus method { return("Hello " + name + " A very Good Morning"); }; // string result = Greetings("Atul"); //string str = Greetings("Atul"); string str = obj.Invoke("Atul"); Console.WriteLine(str); Console.ReadLine(); }
public void ExcuteCompleted(IAsyncResult IResult) { Thread.Sleep(5000); Console.WriteLine("AddComplete running on thread {0}", Thread.CurrentThread.ManagedThreadId); //获取绑定函数的引用 AsyncResult ar = (AsyncResult)IResult; GreetingsDelegate delBp = (GreetingsDelegate)ar.AsyncDelegate; //等待函数执行完毕 string result = delBp.EndInvoke(IResult); Console.WriteLine("5 + 5 ={0}", result); }
static void Main() { GreetingsDelegate obj = new GreetingsDelegate(Greetings); GreetingsDelegate obj2 = delegate(string name) // This method is anonymous method. We can use this method too { return("Hello " + name + ", a very good morning."); }; string str = obj.Invoke("Karren"); Console.WriteLine(str); Console.WriteLine(); str = obj2.Invoke("Kannon"); Console.WriteLine(str); }
static void Main(string[] args) { // GreetingsDelegate obj = new GreetingsDelegate(Greetings); GreetingsDelegate obj = delegate(string name) { return("hello" + " " + name + " " + "Very Good Morning"); }; string str = obj.Invoke("Aishwarya"); Console.WriteLine(str); Console.ReadLine(); }
static void Main(string[] args) { Program obj = new Program(); //The add method is non static so AddDelegate ad = new AddDelegate(obj.Add); GreetingsDelegate gd = new GreetingsDelegate(Program.Greetings); //calling non static through object ad(100, 100); string greetingMessage = gd("test"); Console.WriteLine(greetingMessage); Console.ReadKey(); }
static void Main(string[] args) { numsDelegate p = new numsDelegate(Addnums); numsDelegate m = new numsDelegate(multinums); numsDelegate v = new numsDelegate(divnums); GreetingsDelegate g = new GreetingsDelegate(Greetings); GreetingsDelegate w = new GreetingsDelegate(Wishes); p.Invoke(100, 50); m.Invoke(500, 600); v.Invoke(800, 9000); g.Invoke("Hari"); w.Invoke("wish you "); Console.ReadLine(); }
// public static string Greetings(string name) //{ // return "hello" + name + "a very good morning"; //} static void Main(string[] args) { // GreetingsDelegate gd = new GreetingsDelegate(Greetings); //GreetingsDelegate obj = delegate (string name) //{ // return "hello" + name + "a very good morning"; //}; GreetingsDelegate obj = (name) => { return("hello" + name + "a very good morning"); }; string str = obj.Invoke("Aish"); Console.WriteLine(str); Console.ReadLine(); }
static void Main(string[] args) { //Instantiate the delegate GreetingsDelegate obj = delegate(string name) { return(" Hello " + name + " Very Good Morning "); }; GreetingsDelegate obj1 = delegate(string name) { return(" This is " + name + " Method "); }; AddDelegate obj2 = delegate(int a, int b) { return(a + b); }; MulDelegate obj3 = delegate(int x, int y) { return(x - y); }; //Method is value returning Method string str = obj.Invoke("Guys"); Console.WriteLine(str); string str1 = obj1.Invoke("Anonymouus"); Console.WriteLine(str1); int result = obj2.Invoke(10, 20); Console.WriteLine(result); int result1 = obj3.Invoke(40, 20); Console.WriteLine(result1); Console.ReadLine(); }
static void Main(string[] args) { var obj = new Delegates01(); //Instantiating delegate by passing the target function Name //The Add method is non static so here we are calling method using object AddDelegate ad = new AddDelegate(obj.Add); //Greetings method is static so here we are callling the method by using the class name GreetingsDelegate gd = new GreetingsDelegate(Delegates01.Greetings); //Invoking The Delegates ad(100, 50); string GreetingsMessage = gd("Pranaya"); //We can also use Invoke method to execute delegates // ad.Invoke(100, 50); // string GreetingsMessage = gd.Invoke("Pranaya"); Console.WriteLine(GreetingsMessage); Console.ReadKey(); }
static void Main(string[] Args) { //List to store numbers List <int> numbers = new List <int>() { 12, 24, 36, 48, 567, 72, 78, 96, 32, 120, 567, 678 }; Console.WriteLine("The elements of List are:"); //using Lambda expressions to calculate sum of each number in the list(z =>z + z+ z) var sum = numbers.Select(z => z + z + z); foreach (var num in numbers) { Console.WriteLine("{0}", num); } Console.WriteLine(); //using Lambda expressions to calculate square of each number in the list(x => x*x) Addnums2Delegate obj2 = (x, y, z) => { Console.WriteLine(x + y + z); }; obj2.Invoke(10, 3.142f, 123456.7809738);//Non return type GreetingsDelegate w = (wish) => { Console.WriteLine(wish + " " + "Happy Birthday!!!!!!"); }; Console.ReadKey(); //using Lambda expressions to calculate cube of each number in the list(x => x*x*x) var qube = numbers.Select(y => y * y * y); foreach (var number in qube) { Console.WriteLine("{0}", number); } Console.ReadKey(); }
static void Main(string[] Args) { List <int> numbers = new List <int>() { 01, 04, 09, 12, 13, 18, 30 }; Console.WriteLine("The elements of List are:"); var sum = numbers.Select(z => z + z + z); foreach (var num in numbers) { Console.WriteLine("{0}", num); } Console.WriteLine(); Addnums2Delegate obj1 = (x, y, z) => { Console.WriteLine(x + y + z); }; obj1.Invoke(13, 3.142f, 123456.123456789); GreetingsDelegate w = (wish) => { Console.WriteLine(wish + " " + "Happy Birthday Jimin!!!!!!"); }; Console.ReadKey(); var qube = numbers.Select(y => y * y * y); foreach (var number in qube) { Console.WriteLine("{0}", number); } Console.ReadKey(); }
static void Main(string[] args) { string fav = "Dog"; //Instantiate the delegate NumberDelegate obj = delegate(int x) { Console.WriteLine("Anonymous Method:{0}", x); }; obj(20); obj = new NumberDelegate(Addnums); obj(10); obj = new NumberDelegate(Mulnums); obj(7); PetDelegate p = delegate(string mypet) { Console.WriteLine("My Favorite pet is {0}", mypet); Console.WriteLine("And I Like {0} aslo", fav); }; p("Cat"); GreetingsDelegate obj1 = delegate(string name) { Console.WriteLine(" Hello Friends " + name + ""); }; obj1("Good Noon"); Console.ReadLine(); }