Skip to main content

Unit 3 Array In C# (Class 12)

             Unit 3 Array in C#

Introduction

An array is a collection of variables of the same data type, stored in contiguous memory locations, and accessed using a common name and index.

Each item in an array is called an element, and the index of arrays in C# starts from 0.

 

 Key Points About Arrays in C#:

1.      Elements are stored in contiguous memory locations.

2.      Index starts from 0. So, for an array of size 5, valid indexes are 0 to 4.

3.      Arrays are reference types and are allocated on the heap.

4.      C# array is an object of base type System.Array.

5.      Array elements can be of any type, including another array (array of arrays).

6.      Jagged array (array of arrays) elements are reference types and are initialized to null.

7.      Arrays can be single-dimensional, multi-dimensional (rectangular), or jagged.

8.      The size of an array is fixed after its declaration.

Types of Arrays in C#:

Type

Description

Example Syntax

Single-Dimensional

One row of elements

int[] arr = new int[5];

Multi-Dimensional

Array with multiple rows and columns

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

Jagged Array

Array of arrays (different lengths allowed)

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



Declaration and Initialization of Arrays in C#

In C#, an array must first be declared before it can be used, and then it must be initialized to allocate memory and optionally assign values.

Declaration of an Array

An array is declared by specifying the data type of its elements, followed by square brackets [], and the array name.

 Syntax:

datatype[] arrayName;

This statement declares a reference to an array of the specified data type but does not allocate memory for the elements.

 Example:

int[] numbers;
string[] names;

In the above examples, numbers is declared as an array of integers, and names is an array of strings.

2. Initialization of an Array

After declaration, an array must be initialized using the new keyword to allocate memory.

There are several ways to initialize an array in C#:

 a. Using the new keyword with a fixed size

This method allocates memory for a specific number of elements. The elements will be initialized to their default values (e.g., 0 for int, null for string).

int[] numbers = new int[5];  
 // Creates an array of 5 integers with default values (0)

b.  Assigning values individually using index

After initializing the array, values can be assigned one by one using the index position.

numbers[0] = 10;
numbers[1] = 20;

 c. Initialization with values at the time of declaration

You can directly assign values to the array during declaration.

int[] numbers = { 10, 20, 30, 40, 50 };

In this case, the size of the array is automatically determined based on the number of values.

 d. Using new keyword with values

Here, the array is explicitly created using new, and values are assigned at the same time.

int[] numbers = new int[] { 10, 20, 30, 40, 50 };

 e. Specifying size and values together

You can also specify both the size and the initial values.

int[] numbers = new int[5] { 10, 20, 30, 40, 50 };

Here, the number of elements in the initializer must match the specified size.

 f. Creating an empty array and assigning later

This approach is useful when the values are not known at the time of declaration.

string[] names = new string[3];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";

Comments

Popular posts from this blog

Unit-1 Introduction to C#.NET (Class 12)

  What is .NET Framework? The .NET Framework is a software development platform developed by Microsoft. It provides tools and libraries to build and run Windows applications, web services, and web apps. It gives tools and libraries that make it easier to write programs. It also helps the computer run those programs safely and efficiently. Microsoft started working on .NET Framework in the late 1990s . The first version, .NET Framework 1.0 , was released in 2002 .   The .NET Framework is made up of several important components: 1.       Common Language Runtime (CLR) is the core engine that runs your program. It converts your code into machine code that the computer can understand, manages memory, handles errors, and ensures that your program runs safely. 2.       The Class Library is a large collection of ready-made code that helps you perform common tasks like working with files, databases, graphics, the i...

Unit 2 Operating System

  Unit- 2  Process and Process Scheduling 2.1 Introduce Process, Program and Process Life Cycle  Process Process is something that is currently under execution. So, an active program can be called a Process. Examples: ●        Opening a web browser to search something on the internet — the browser becomes a process. ●        Launching a music player to enjoy your favorite tunes — the music player is also a process. In computing, a process is the instance of a computer program that is being executed by one or many threads. It contains the program code and its activity. Modern operating systems support multithreading , meaning a process can have multiple threads running concurrently .   A Process has various attributes associated with it. Some of the attributes of a Process are: ●          Process Id: Every process will be given a unique id that identifies the process...

Introduction to Software Engineering (Class 12)

 Introduction to Software Engineering Software engineering is the branch of computer science that deals with the design, development, testing, and maintenance of software applications. Software engineers apply engineering principles and knowledge of programming languages to build software solutions for end users. IEEE, in its standard 610.12-1990, defines software engineering as the application of a systematic, disciplined, which is a computable approach for the development, operation, and maintenance of software. Boehm defines software engineering, which involves, ‘the practical application of scientific knowledge to the creative design and building of computer programs. It also includes associated documentation needed for developing, operating, and maintaining them.’ Importance of Software Engineering Reduces complexity Large software systems are divided into smaller, manageable modules. Each module is developed and solved independently, ...