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() { //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) { //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); }
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(); }
//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) { 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(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(); }
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 gdel = delegate(string name) { return("Hello @" + name + "Welcome to Dotnet Tutorials"); }; string GreetingsMessage = gdel.Invoke("Pranaya"); Console.WriteLine(GreetingsMessage); }
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(); }
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(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() { //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(); }
//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(); }
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) { 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) { MultiplynumsDelegate obj = new MultiplynumsDelegate(Multiplynums); double result1 = obj.Invoke(10, 20);//New variable to store the value -return type Console.WriteLine(result1); GreetingsDelegate w = new GreetingsDelegate(Wishes); w.Invoke("happy grduation "); CheckLengthDelegate obj1 = new CheckLengthDelegate(CheckLength);//New variable to store the value -return type bool check = obj1.Invoke("dhronacharya"); Console.WriteLine(check); Console.ReadLine(); }
static void Main(string[] args) { //GreetingsDelegate obj = new GreetingsDelegate(Greetings); GreetingsDelegate obj = delegate(string name) { return("Hello " + name + " a very good morning."); }; Func <string, string, string> obj1 = delegate(string n, string n1) { return("Hello " + n + " " + n1); }; Action <int, int> oo = delegate(int a, int b) { Console.WriteLine(a + b); }; oo(100, 99); string str = obj.Invoke("Nitesh"); Console.WriteLine(obj1.Invoke("Nitesh", "Kushwaha")); Console.WriteLine(str); Console.ReadKey(); }
/*//Method with return value * public static int Multiplynums(int x, int y) * { * return (x * y); * } * * public static string Quick(string name) * { * return "Hello" + name; * } * * * //method without return value * public static void Addnums2(int x, float y, double z) * { * Console.WriteLine(x + y + z); * } * //method without return value * public static void Wishes(string wish) * { * Console.WriteLine(wish + " " + "Happy Birthday!!!!!!"); * } * * * public static bool CheckLength(string name) * { * //check the string whose length is greater than 4 should print true else false; * if (name.Length > 4) * return true; * return false; * } * public static bool value(string name1) * { * //check the string whose length is greater than 4 should print true else false; * if (name1.Length < 10) * return true; * return false; * }*/ 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 "); CheckLengthDelegate obj3 = delegate(string name1) { //check the string whose length is greater than 4 should print true else false; if (name.Length > 4) { return(true); } return(false); }; bool check = obj3.Invoke("Haritha"); Console.WriteLine(check); CheckLengthDelegate o = delegate(string name1) { //check the string whose length is greater than 4 should print true else false; if (name1.Length < 10) { return(true); } return(false); };//New variable to store the value -return type bool ch = o.Invoke("Maredapaaly"); Console.WriteLine(ch); Console.ReadLine(); }
//01. in previous session, we saw an ananymonous method means a method with out a method body which can be bound directly to // a delegate and can be called //02. first the named method was created, then the named method was binded with a delegate //03. afterwards the named method was changed to an anonymonous method and then it was invoked in the process //04. anonymonous method, the advantage is lesser typing work, due to no need to use private|public, static, // return type or the fn name //05. we directly use it with delegate keywork and directly we use it //06. (imp) lamda express is a short hand for writing the anonymonous methods //07. lamba express was still introduced to simplify the process of anonymonous method //08. lets use the same code that we have used in previous Anonymonous method class //11. create the method (Greetings) //15. comment the below method, as we have written it as an anonymonous method //////public static string Greetings(string name) //////{ ////// return "Hello " + name + ", very good morning"; //////} //12. create the main method, instinciate the delegate and then invoke the delegate by passing an argument //13. run it and we get the output as: Hello Raju, very good morning public void NDelegateMain() { //14. now lets convert it as anonymonous method and comment or remove the method (Greetings) //GreetingsDelegate objGreetingsDelegate = new GreetingsDelegate(Greetings); //////GreetingsDelegate objGreetingsDelegate = delegate (string name) { ////// return "Hello " + name + ", very good morning"; //////}; //15. now if we try to run the program, we will get the same output //OP: Hello Raju, very good morning //16. here we just need to tell the parameter along with type of prameter to the Delegate and all // the above we have seen in previous class //17. no need to tell the return type, because the 'GreetingsDeledate' is very much clear that it returns a string // public delegate string GreetingsDelegate(string name); //18. here a doubt arises, since delegate know the return type no need to tell the return type but delegate also knows the // parameter then why need to tell the delegate again all these things when it knows //19. ananymonous method it self is telling (typing lesser code) but we need to type delegate and parameter type //20. in c# 2.0 anonymonous method was introduced in the above format of writing the code //21. here a doubt arises, since delegate know the return type no need to tell the return type but delegate also knows the // parameter then why need to tell the delegate again all these things when it knows //22. hence in c# 3.0, they introduced a concept call lamda expressions, with lamda expressions the code can be written in a\ // much simplified way //23. lamda express is a short hand for writing anonymonous methods //24. with lamda express, there is no need type the delegate keyword and write the parameter type also //25. straight away we are provided with one operator called lambda operator => //26. lets modify the above anonymonous call (in line: 48) to lambda expression //28. this is still the simiplified version of anonymonous method //29. in lambda expression we can also tell the parameter type as (string) it works, but still it is not required // because delegate already knows the parameter type it is going to take //GreetingsDelegate objGreetingsDelegate = (string name) => //30. to understand lambda expression we need to first understand anonymous method, the anonymonous methods are // simplified with the concept of lambda expressions GreetingsDelegate objGreetingsDelegate = (name) => { return("Hello " + name + ", very good morning"); }; //27. for comparision with anonymonous method /* * GreetingsDelegate objGreetingsDelegate = delegate (string name) * { * return "Hello " + name + ", very good morning"; * }; */ string returnVal = objGreetingsDelegate.Invoke("Raju"); Console.WriteLine(returnVal); }