LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Arrays and Strings in C#

Strings help us work with names, messages and other text. Let's understand how to create, access and process them using C#.

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
Remember Array indexes start at 0. An array with three elements has indexes 0, 1 and 2.

4Let's Write the C# Program

We will create a student array and perform a few string operations.

Program.csC#
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());
    }
}
Pro Tip Use a loop with an array when you need to process every element without writing repeated statements.

5What Does the Program Print?

The first array item is Asha. The message contains eight characters, including the space.

Output
First student: Asha
Total students: 3
Message: Hello C#
Length: 8
Uppercase: HELLO C#
Common Mistake Accessing an index outside the array range causes an IndexOutOfRangeException at runtime.

6Watch Arrays and Strings in Action

See how the program reads an array item, checks its length and processes a string.

● ● ●Program.cs - Visual Studio Code
▸ Program.cs
▣
⌕
⑂
⚙
 
PROBLEMS  OUTPUT  DEBUG CONSOLE  TERMINAL
 
 
 
Ln 16, Col 9  UTF-8  C#

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

Q1Easy

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.

Q2Easy

What is the first index of an array?

The first index is zero. An array with five elements has indexes from 0 to 4.

Q3Easy

How do you find an array's size?

Use the Length property. For example, students.Length returns the number of elements.

Q4Medium

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.

Q5Medium

Are strings mutable in C#?

No. Strings are immutable, meaning their contents cannot be changed after creation. String operations create new string values.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.