Professional Documents
Culture Documents
NET
Types
Value Data Type
The value data types are integer-based and floating-point based. C# language supports both
signed and unsigned literals.
C# Structs
In C#, classes and structs are blueprints that are used to create instance of a class. Structs are used
for lightweight objects such as Color, Rectangle, Point etc.
Unlike class, structs in C# are value type than reference type. It is useful if you have data that is
not intended to be modified after creation of struct.
Example
Let's see a simple example of struct Rectangle which has two data members width and height.
using System;
public struct Rectangle
{
public int width, height;
}
public class TestStructs
{
public static void Main()
{
Rectangle r = new Rectangle();
r.width = 4;
r.height = 5;
Console.WriteLine("Area of Rectangle is: " + (r.width * r.height));
}
}
Output:
C# Enum
Enum in C# is also known as enumeration. It is used to store a set of named constants such as
season, days, month, size etc. The enum constants are also known as enumerators. Enum in C#
can be declared within or outside class and structs.
Enum constants has default values which starts from 0 and incremented to one by one. But we
can change the default value.
Points to remember
o enum has fixed set of constants
o enum improves type safety
o enum can be traversed
Example
using System;
public class EnumExample
{
public enum Season { WINTER, SPRING, SUMMER, FALL }
public static void Main()
{
int x = (int)Season.WINTER;
int y = (int)Season.SUMMER;
Console.WriteLine("WINTER = {0}", x);
Console.WriteLine("SUMMER = {0}", y);
}
}
Output:
WINTER = 0
SUMMER = 2
C# Arrays
Like other programming languages, array in C# is a group of similar types of elements that have
contiguous memory location. In C#, array is an object of base type System.Array. In C#, array
index starts from 0. We can store only fixed set of elements in C# array.
Advantages of C# Array
o Code Optimization (less code)
o Random Access
o Easy to traverse data
o Easy to manipulate data
o Easy to sort data etc.
Disadvantages of C# Array
o Fixed size
C# Array Types
int[] arr = new int[5];//creating array
int arr[] = new int[5];//compile time error
Let's see a simple example of C# array, where we are going to declare, initialize and
traverse array.
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = new int[5];//creating array
arr[0] = 10;//initializing array
arr[2] = 20;
arr[4] = 30;
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
Output:
10
0
20
0
30
int[] arr = new int[5]{ 10, 20, 30, 40, 50 };
int[] arr = new int[]{ 10, 20, 30, 40, 50 };
int[] arr = { 10, 20, 30, 40, 50 };
Let's see the example of array where we are declaring and initializing array at the same time.
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = { 10, 20, 30, 40, 50 };//Declaration and Initializatio
n of array
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
Output:
10
20
30
40
50
We can also traverse the array elements using foreach loop. It returns array element one by one.
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = { 10, 20, 30, 40, 50 };//creating and initializing arr
ay
//traversing array
foreach (int i in arr)
{
Console.WriteLine(i);
}
}
}
Output:
10
20
30
40
50
Multidimensional Arrays
The multidimensional array is also known as rectangular arrays in C#. It can be two dimensional
or three dimensional. The data is stored in tabular form (row * column) which is also known as
matrix.
To create multidimensional array, we need to use comma inside the square brackets. For
example:
int[,] arr=new int[3,3];//declaration of 2D array
int[,,] arr=new int[3,3,3];//declaration of 3D array
Example
Let's see a simple example of multidimensional array in C# which declares, initializes and
traverse two-dimensional array.
using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr=new int[3,3];//declaration of 2D array
arr[0,1]=10;//initialization
arr[1,2]=20;
arr[2,0]=30;
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
Output:
0 10 0
0 0 20
30 0 0
int[,] arr = new int[3,3]= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[,] arr = new int[,]{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Let's see a simple example of multidimensional array which initializes array at the time of declaration.
using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//
declaration and initialization
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
Output:
1 2 3
4 5 6
7 8 9
Jagged Arrays
In C#, jagged array is also known as "array of arrays" because its elements are arrays. The
element size of jagged array can be different.
Let's see an example to declare jagged array that has two elements .
int[][] arr = new int[2][];
Let's see an example to initialize jagged array. The size of elements can be different.
arr[0] = new int[4];
arr[1] = new int[6];
arr[0] = new int[4] { 11, 21, 56, 78 };
arr[1] = new int[6] { 42, 61, 37, 41, 59, 63 };
Here, size of elements in jagged array is optional. So, you can write above code as given
below:
arr[0] = new int[] { 11, 21, 56, 78 };
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };
Example
Let's see a simple example of jagged array in C# which declares, initializes and traverse jagged
arrays.
public class JaggedArrayTest
{
public static void Main()
{
int[][] arr = new int[2][];// Declare the array
arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };
// Traverse array elements
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write(arr[i][j]+" ");
}
System.Console.WriteLine();
}
}
}
Output:
11 21 56 78
42 61 37 41 59 63
int[][] arr = new int[3][]{
new int[] { 11, 21, 56, 78 },
new int[] { 2, 5, 6, 7, 98, 5 },
new int[] { 2, 5 }
};
Example
public class JaggedArrayTest
{
public static void Main()
{
int[][] arr = new int[3][]{
new int[] { 11, 21, 56, 78 },
new int[] { 2, 5, 6, 7, 98, 5 },
new int[] { 2, 5 }
};
// Traverse array elements
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write(arr[i][j]+" ");
}
System.Console.WriteLine();
}
}
}
Output:
11 21 56 78
2 5 6 7 98 5
2 5
C# dynamic array
C# supports both static and dynamic arrays. If you're new to arrays, check out Working
With Arrays in C#.
A static array has a fixed size and defined when an array is declared. The following code
defines an array that can hold 5 int type data only.
1. int[] odds = new int[5];
Arrays in C# are 0th index. That means the first item of an array starts at the 0th
position. The position of the last item on an array will total number of items - 1. So, if an
array has 5 items, the last item of the array is access by using index 4.
1. odds[0] = 1;
2. odds[1] = 3;
3. odds[2] = 5;
4. odds[3] = 7;
5. odds[4] = 9;
In a fixed array, if you try to add more items than its range, you will get the following
error:
1. odds[5] = 11;
A dynamic array does not have a predefined size. The size of a dynamic array increases
as you add new items to the array. You can declare an array of fixed length or dynamic.
You can even change a dynamic array to static after it is defined. The following code
snippet declares a dynamic array where the size of the array is not provided.
1. int[] numArray = new int[] {};
Dynamic arrays can be initialized as static arrays. The following code snippet declares a
dynamic array and initializes.
1. int[] numArray = new int[] { 1, 3, 5, 7, 9, 11, 13 };
The following code sample declares 3 dynamic arrays of different data types.
1. // Dynamic array of int type
2. int[] numArray = new int[] {1, 3, 5, 7, 9, 11 };
3. // Dynamic array of string type
4. string[] strArray = new string[] { "Mahesh Chand",
5. "David McCarter", "Allen O'neill", "Chris Love" };
6. // Dynamic array of a collection
7. List<string> authors = new List<string>();
8. authors.Add("Mahesh Chand");
9. authors.Add("David McCarter");
10. authors.Add("Allen O'neill");
11. authors.Add("Chris Love");
We can access both dynamic arrays or static arrays same way by passing the item index
in the array.
1. // Dynamic array of int type
2. int[] numArray = new int[] {1, 3, 5, 7, 9, 11 };
3. Console.WriteLine(numArray[0]);
4. Console.WriteLine(numArray[1]);
5. Console.WriteLine(numArray[2]);
6. Console.WriteLine(numArray[3]);
7. Console.WriteLine(numArray[4]);
8. Console.WriteLine(numArray[5]);
1. // Dynamic array of string type
2. string[] strArray = new string[] { "Mahesh Chand",
3. "David McCarter", "Allen O'neill", "Chris Love" };
4. foreach (string str in strArray)
5. Console.WriteLine(str);
Unless you limit an array size, the default arrays are dynamic arrays. Dynamic arrays are
used when you’re not sure about the number of elements an array can store. Static
arrays should be used when you do not want an array to hold more items that its
predefined size.
ArrayList collection
The ArrayList class is often used to work with a collection of objects. It is a dynamic
collection and provides built-in methods to work with list items such as add items,
remove items, copy, clone, search, and sort array.