Category: C#

declaring a delegate array

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

delegate with multiple arguments

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

delegate static and instance methods

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()

delegate simple example

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 //

implict – explicit type casting

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

matrices computations using operator overloading

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++)

user defined casts

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;

operator overloading simple example

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

The “is” operator

The “is operator” is used to check the type of a variable/object. The following example shows how to do it: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 { class Person { } class Program { static void Main(string args) { int i = 10; if(i is object) { Console.WriteLine("i is

The “as” operator

“as” operator is used to cast some parent type to child type. For example, to cast from object to string, in the following example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 { class Program { static void Main(string args) { object o1 = "Some string"; object o2 = 5; string