Docsity

Tuples and Arrays in C sharp, Summaries of C Sharp Programming

Partial preview of the text

Download Tuples and Arrays in C sharp and more Summaries C Sharp Programming in PDF only on Docsity!

Dr. Farman Ullah (CUI Abbottabad)

Advanced Programming

BCS- 5

Advanced C# Concepts

Arrays and Tuples

Syntax

int [] numbers = new int [5];

Key Points

Fixed size

Same data type

Reference type

Default values assigned

 Declare

 Allocate

 Initialize

 Access and assign

 Access through foreach loop

int [] primes ;

primes = new int [10];

int [] primes = new int [9];

int [] prime = new int [] { 1, 2, 3, 5, 7, 11, 13, 17, 19 };

int [] prime = { 1, 2, 3, 5, 7, 11, 13, 17, 19 }; //(valid)

Var prime = { 1, 2, 3, 5, 7, 11, 13, 17, 19 }; //( invalid )

int num = prime[2];

foreach ( int i in prime) Console. WriteLine ( i );

 Index from End (^ Operator)

 Array Comparison Issue

 Multidimensional Arrays

 Jagged Arrays

int last = numbers[^1];

Accesses the last element of the array using the index-from-end operator (^).

if (arr1 == arr2) // compares reference

int [,] matrix = new int [2, 3];

int [][] jagged = new int [3][];

 var ( Implicitly Typed Local Variable )

var x = new int[] {3, 4, 5, 6}; // Correct var x = new[] {3, 4, 5, 6}; // Correct (implicitly inferred as int[]) var x = {3, 4, 5, 6}; // Compilation Error

The var keyword requires an explicit type to be inferred, but { 3 , 4 ,

5 , 6 } alone does not provide enough information.

The compiler does not automatically assume it's an array.

Array Collection Expressions (C# 12+)

int [] myArray = [2, 4, 5, 6, 7, 8, 9]; // C# 12+

This syntax is called Collection Expressions (introduced in C# 12 )

It provides a shorter and cleaner way to initialize arrays and collections

Key Features

No need to write new int[]

The compiler automatically infers the type

Makes code more readable and concise

Can be used with arrays, lists, spans, and more

Traditional vs Modern Syntax // Traditional way int [] arr1 = new int [] { 2, 4, 5, 6 }; // C# 12 Collection Expression int [] arr2 = [2, 4, 5, 6];

Understanding Array Reassignment and Data Loss in C# int[] myArray = new int[10]; // Creates an array of 10 elements, all initialized to 0. myArray = [2, 4, 5, 6, 7, 8, 9]; // Reassigns myArray to a new array with 7 elements. var x = {3, 4, 5, 6}; // Compilation Error myArray = [ 2 , 4 , 5 , 6 , 7 , 8 , 9 ]; o This reassigns myArray to a new array containing only 7 elements: [ 2 , 4 , 5 , 6 , 7 , 8 , 9 ]. o The original array of 10 elements is discarded and replaced by this new array. o The original 10 - element array is lost, and now myArray holds only 7 elements.

In C# 8. 0 and later, the ^ (hat) operator is used for indexing from the end of an array or collection. Explanation of nums[^1]:

  • ^1 means "1st element from the end" (i.e., the last element ).
  • ^2 would mean the 2nd element from the end, and so on.
  • It is equivalent to nums[ nums.Length - 1 ] but more concise.

^ (hat) operator

// Declaring and Initializing an array with a size of 5

int[] array = new int[5];

//Defining and assigning elements at the same time

int[] array2 = new int[5]{1,2,3,4,5};

//Initialize with 5 elements will indicate the size of an array

int[] array3 = new int[] { 1, 2, 3, 4, 5 };

// Another way to initialize an array without the size

int[] array4 = { 1, 2, 3, 4, 5 };

// Declare an array without initialization

int[] array5;

array5 = new int[]{ 1, 2, 3, 4, 5 };

int[] array = new int[5]; array[0] = 1; array[1] = 2; array[2] = 3; array[3] = 4; array[4] = 5;

Types of Multidimensional Arrays

Rectangular Array (2D)

A rectangular array has a fixed number of columns for every row.

Declaration & Allocation:

You use a comma inside the square brackets [,] to indicate dimensions.

// A 2D array with 3 rows and 4 columns int [,] matrix = new int [3, 4];

Initialization:

You can fill the array at the time of declaration using nested braces.

int [,] matrix = { {1, 2, 3}, {4, 5, 6} };

Types of Multidimensional Arrays

Accessing Elements:

Use coordinates [ row , column ].

int value = matrix[0, 1];

// Accesses row 0, column 1 (Value: 2)