If there is a single parameter for lambda expression, just name of the parameter is sufficient as shown in below example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication10 { class Program { static void Main(string args) { Func<string, string> oneParam = s => String.Format("change uppercase {0}", s.ToUpper()); Console.WriteLine(oneParam("test")); } }
Lambda expressions allow us to reduce the code, thereby reducing the complexity of the code. Here is an example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication10 { class Program { static void Main(string args) { Func<string, string> lambda = param => { return param + " XYZ "; }; Func<string,
We can also declare a delegate array. However, each element of the delegate array must be of same signature in terms of input arguments and return types. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication10 { class Program { delegate double DoubleOp(double x); static void Main(string args) { /* DoubleOp operations
We can also declare a delegate to receive multiple arguments using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication10 { class Program { delegate double GetInt(int num); delegate void ShowInt(int num); delegate void MultipleArguments(int num1, int num2); static void Main(string args) { GetInt mydelegate = Program.getValue; double ans = mydelegate(45); Console.WriteLine(ans); ShowInt
We can use delegate for static and non static methods, as shown in the following example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication10 { struct Currency { public uint Dollars; public ushort Cents; public Currency(uint dollars, ushort cents) { this.Dollars = dollars; this.Cents = cents; } public override string ToString()
The following is a simple example of declaring and using a delegate. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication10 { class Program { private delegate string GetAString(); static void Main(string args) { int x = 40; GetAString firstStringMethod = new GetAString(x.ToString); // the above line can be written as //
Using operator overloading, we can also perform impliclit/explicit type casting. For example, in the following program, we are going to type cast Currency class object into dollars and cents, and from dollars and cents to Currency. REMEMBER: When converting from user defined type to primitive type, we use “implicit” overloading and conversion, whereas when converting
The following example shows the computations of matrices using operator overloading: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 { class Matrix { int[,] matrix; public Matrix() { } public Matrix(int[,] matrix) { this.matrix = matrix; } public void ShowMatrix() { Console.WriteLine(); for(int i=0; i<matrix.GetLength(0); i++) { for(int j=0; j<matrix.GetLength(1); j++)
We can convert from one data type to another. If we convert from a higher capacity data type to lower capacity datatype, the data loss will occur and this usually goes unnoticed. Therefore, it is always recommended to perform conversion under checked {} blocked, as in the following example: using System; using System.Collections.Generic; using System.Linq;
The operator overloading allows the operators e.g., +, -, /, *, etc to operate on operands of non primitive/basic data types, such as int, float, double, etc. The following example explains operator overloading: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 { class Money { public int money; public int MoneySum