1Working with Arrays and Strings
Hi friends, in this article we will learn about arrays and strings in C#. An array stores multiple values of the same data type in one variable. A string stores text. We use arrays to manage collections such as student marks, product prices and employee names. Strings help us work with names, messages and other text. Let's understand how to create, access and process them using C#.
2Think About a Student List
Imagine a school maintaining a list of student names. Instead of creating separate variables for every student, we can store all names inside one array.
Each student has a position called an index. The first position is index 0. A student's name is text, so each value is a string. We can also find the number of students using the array's Length property.
3Important Things to Understand
1. Declaring an Array
Use the data type followed by square brackets. For example, int[] marks creates an integer array variable.
2. Accessing Array Elements
Use the index to read or update a value. Remember, array indexing starts from zero.
3. Array Length
The Length property gives the total number of elements in an array.
4. Working with Strings
A string is a sequence of characters. Common properties and methods include Length, ToUpper() and ToLower().
| Operation | Purpose |
|---|---|
| names[0] | Access first item |
| names.Length | Count array items |
| text.ToUpper() | Convert to uppercase |
| text.ToLower() | Convert to lowercase |
4Let's Write the C# Program
We will create a student array and perform a few string operations.
using System;
class Program
{
static void Main()
{
string[] students = { "Asha", "Ravi", "Meena" };
Console.WriteLine("First student: " + students[0]);
Console.WriteLine("Total students: " + students.Length);
string message = "Hello C#";
Console.WriteLine("Message: " + message);
Console.WriteLine("Length: " + message.Length);
Console.WriteLine("Uppercase: " + message.ToUpper());
}
}
5What Does the Program Print?
The first array item is Asha. The message contains eight characters, including the space.
First student: Asha Total students: 3 Message: Hello C# Length: 8 Uppercase: HELLO C#
6Watch Arrays and Strings in Action
See how the program reads an array item, checks its length and processes a string.
⌕
⑂
⚙
7Quick Revision
Arrays store multiple values of the same type. Strings store text and provide useful methods for processing it. Both are widely used in everyday application development.
- Array indexes start from zero.
- Use Length to find the number of elements or characters.
- Use string methods to transform text.
8Questions Interviewers May Ask
What is an array in C#?
An array stores multiple values of the same data type in a single variable. Each value is accessed using an index.
What is the first index of an array?
The first index is zero. An array with five elements has indexes from 0 to 4.
How do you find an array's size?
Use the Length property. For example, students.Length returns the number of elements.
What is the difference between an array and a string?
An array stores multiple elements of a specified type. A string represents a sequence of characters.
Are strings mutable in C#?
No. Strings are immutable, meaning their contents cannot be changed after creation. String operations create new string values.