0% found this document useful (0 votes)
8 views8 pages

Csharp Arrays Guide

The document provides an overview of C# arrays and strings, detailing their definitions, usage, and key features. It covers array initialization, accessing elements, looping, and the characteristics of strings, including immutability and built-in methods. Additionally, it discusses control statements, functions, and the object class in C#, highlighting their importance and advantages.

Uploaded by

princevermaji63
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Csharp Arrays Guide

The document provides an overview of C# arrays and strings, detailing their definitions, usage, and key features. It covers array initialization, accessing elements, looping, and the characteristics of strings, including immutability and built-in methods. Additionally, it discusses control statements, functions, and the object class in C#, highlighting their importance and advantages.

Uploaded by

princevermaji63
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C# Arrays & Strings

1 Aug 2025 | 11 min read

In the C# programming language, an array is a collection of the same or different types of data items in a
single variable. It works on the basis of an index. The array start index is 0, and the last element of the index
is size - 1.

Array Short Notes


• Definition: Array is a fixed-size sequential collection of elements of the same type.
• Indexing: Starts from 0.
• Declaration:

int[] arr; // declaration


arr = new int[5]; // initialization

• Initialization with values:

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

• Accessing elements:

int x = nums[2]; // 3

• Length Property:

int len = [Link];

• Looping (for):

for (int i = 0; i < [Link]; i++)


[Link](nums[i]);

• Looping (foreach):

foreach (int item in nums)


[Link](item);

1
• Multidimensional Array:

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


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

• Jagged Array: Array of arrays

int[][] jagged = new int[2][];


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

• Common Use-Cases:
• Storing fixed number of related values
• Easy access via index
• Used in loops, data structures, and algorithms

C# Strings (Short Notes)


A string is a sequence of characters enclosed in double quotes. C# provides many built-in methods to
manipulate strings.

Declaring and Initializing

string str1 = "Hello";


string str2 = "World";

String Concatenation

string result = str1 + " " + str2;


[Link](result); // Output: Hello World

String Interpolation

string name = "Alice";


[Link]($"Hello, {name}!");

Useful String Methods

string s = " Hello C# ";


[Link] // Returns 11

2
[Link]() // " HELLO C# "
[Link]() // " hello c# "
[Link]() // "Hello C#"
[Link](2, 5) // " Hell"
[Link]("C#", "World") // " Hello World "

String Comparison

string a = "apple";
string b = "Apple";
bool equal = [Link](b, [Link]); // true

String Split and Join

string data = "a,b,c";


string[] parts = [Link](','); // { "a", "b", "c" }
string joined = [Link]("-", parts); // "a-b-c"

Loop Through Characters

string txt = "Code";


foreach (char c in txt)
{
[Link](c);
}

String is Immutable

Strings are immutable; any change creates a new string.

Work of foreach Loop in C#


The foreach loop is used to iterate through elements in a collection (like arrays, strings, lists, etc.)
without using an index.

Syntax:

foreach (datatype variable in collection)


{

3
// code block
}

Example with Array:

int[] numbers = { 1, 2, 3, 4 };
foreach (int num in numbers)
{
[Link](num);
}

Output:

1
2
3
4

Example with String:

string word = "Hello";


foreach (char ch in word)
{
[Link](ch);
}

Key Points:

• No need to use index


• Read-only loop (you can’t modify original data inside foreach )
• Works with arrays, strings, collections (like List, Dictionary, etc.)

Control Statements Short Notes


Control statements manage the flow of a program based on conditions and loops.

1. Conditional Statements

• if statement

if (condition) { /* code */ }

4
• if...else statement

if (condition) { } else { }

• else if ladder

if (a > b) { }
else if (a == b) { }
else { }

• switch statement

switch (value)
{
case 1: /* code */ break;
case 2: /* code */ break;
default: /* code */ break;
}

2. Looping Statements

• for loop

for (int i = 0; i < 5; i++) { }

• while loop

while (condition) { }

• do...while loop

do { } while (condition);

• foreach loop — explained above

3. Jump Statements

• break : Exit loop/switch early


• continue : Skip current iteration
• return : Exit method and return a value
• goto : Jump to a labeled statement (rarely used)

5
Function Short Notes in C#
Functions (also called methods) are blocks of code that perform a specific task and can be reused.

Syntax

returnType FunctionName(parameters)
{
// code
return value;
}

Example

int Add(int a, int b)


{
return a + b;
}

Calling a Function

int result = Add(5, 3); // result = 8

Types of Functions

• Void Function: Returns nothing

void SayHello() { [Link]("Hello"); }

• Parameterised Function: Accepts input


• Return Type Function: Returns a value
• Static Function: Belongs to class
• Instance Function: Belongs to object

Key Points

• Increases code reusability


• Easy to test and debug
• Breaks code into smaller modules

6
Object Class Short Notes in C#
In C#, the object class is the base class from which all other types (value and reference) directly or
indirectly inherit.

Key Features:

• Located in System namespace.


• Provides basic methods like:
• ToString() → returns string representation
• Equals() → compares two objects
• GetHashCode() → returns hash code
• GetType() → returns runtime type

Example:

object obj = 42;


[Link]([Link]()); // Output: "42"
[Link]([Link]()); // Output: System.Int32

Why Important:

• Enables polymorphism
• Can store any type (int, string, array, etc.)
• Used as a generic type in collections and methods

Advantages of C# String
• Easy to declare and manipulate
• Rich built-in methods
• Supports formatting and comparison

Disadvantages
• Immutable (memory overhead on large modifications)

FAQs
1) Is string a reference type? Yes.

2) How to compare strings ignoring case? Use Equals(str1, str2,


[Link])

7
3) Can strings be null or empty? Yes.

4) How to check if string is empty? [Link](str) or


[Link](str)

(Other array content continues below...)

Simple Array Example in C#

using System;
class Program
{
static void Main()
{
int[] num_bers = { 10, 20, 30 };
for (int i = 0; i < num_bers.Length; i++)
{
[Link]("Element at index " + i + " is: " +
num_bers[i]);
}
}
}

Output:

Element at index 0 is: 10


Element at index 1 is: 20
Element at index 2 is: 30

...

You might also like