Category: C#

checked keyword

Using checked keyword, we can force the compile to throw an exception if a data type is filled with data beyond its capacity. In the following example, the maximum capacity of byte data type is 255. However, if we add more values in it, this will lead to storage of garbage value in it as

Tuples in C#

In C# tuples allow us to store data of various types in a tuple object. Tuple is also useful in returning multiple values from a function. Here is a complete example of tuple: 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) { Tuple<int,

Array segments

Sometimes we want to split an array in various segments and perform some processing on each of segment. ArraySegment class is useful in that. In below program we create a segment array by combining subsets of elements from both arrays ar1 and ar2. First 3 elements of ar1 is picked and element 4, 5, 6

Sorting an array of objects

If we need to sort an array of objects, then  the Array class’s defaut Sort message will not work as it is designed for primitive data types, like double, int, string, float etc. In that case, we need to implement the IComparable interface and define CompareTo method to perform the sorting. In the following, we

Copy one array in another

To copy one array in anthor in C#, we can use the Clone() function of Array class: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication7 { class Program { static void Main(string args) { int myarray1= { 10, 20, 30 }; int myarray2 = (int)myarray1.Clone(); Console.WriteLine("\nArray1"); foreach (int item in myarray1)

Search number in a 3-dim array

In the following example, the user will declare and intialize a 4,3,2 array and search a  number in that array: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication7 { class Program { static void Main(string args) { int[,,] myArray= { { {1,2}, {3,4}, {5,6} }, { {7,8 }, {9,10 }, {11,12

2-dim array

The two dimensional arrays in C# are simple row by column matrix. The following is an example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Collections; namespace ConsoleApplication6 { class Program { static void Main(string args) { int[,] twodim = new int[3, 4]; twodim[0, 0] = 19; twodim[0, 1] =

Three dimensional arrays

The three dimensional arrays can be visualized as boxes within boxes. There is a big box, in which the other boxes are arranged in rows. In each row box, there can be other boxes, and finally, in the enclosed box will be values that are array elements. Here is an example of 3-dim (3,2,2) array:

Jagged Arrays

Using Jagged Arrays, we can define any number of columns within a row of a two/multi-dimensional array. Here is an example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Collections; namespace ConsoleApplication6 { class Program { static void Main(string args) { int jagged = new int[3]; jagged[0] = new int[2]

ArrayList

In C# ArrayList is a very powerful data structure using which we can assign items of any data type within the same array. Following is an example:   using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Collections; namespace ConsoleApplication6 { class Program { static void Main(string args) { var list