Exam Notes

icon picker
Week 3: Methods and Collections

1. Methods in C#

Definition: A method is a reusable block of code designed to perform a specific task. It can accept parameters, execute code, and optionally return a value.
Syntax of a Method:

returnType MethodName(parameterList) {
// Method body
}

Example:

public int Add(int a, int b) {
return a + b;
}

Key Components of a Method:
Return Type: Specifies the type of value the method returns. If a method doesn’t return a value, use void.
Method Name: Follows PascalCase naming convention.
Parameters: Define what arguments the method takes (optional).
Return Statement: Ends the method’s execution and returns a value if specified.
Calling a Method:

int result = Add(3, 4); // Calls the Add method with arguments 3 and 4

Void Methods:
void methods do not return a value.
Example:

public void DisplayMessage() {
Console.WriteLine("Hello, World!");
}

2. Method Overloading

Definition: Method overloading allows defining multiple methods with the same name but different parameter lists.
Key Rule: Overloaded methods must differ in their parameter types, number of parameters, or both.
Example:

public int Add(int a, int b) {
return a + b;
}

public double Add(double a, double b) {
return a + b;
}

MCQ Tip: Watch out for syntax differences. Questions may include small variations, like missing parameter types or incorrect return types.

3. Parameters in Methods

Value Parameters:
By default, parameters are passed by value, meaning the method receives a copy of the argument.
Reference Parameters (ref):
Definition: Allows a method to modify the original variable by passing it by reference.
Usage: The ref keyword must be used both in the method declaration and when calling the method.
Example:
public void UpdateValue(ref int x) {
x += 10;
}
int number = 5;
UpdateValue(ref number); // number becomes 15

Out Parameters (out):
Definition: Used to return multiple values from a method. The variable must be assigned in the method before returning.
Usage: out is specified in both method definition and call.
Example:
csharp
Copy code
public void GetValues(out int a, out int b) {
a = 10;
b = 20;
}
int x, y;
GetValues(out x, out y); // x becomes 10, y becomes 20

Optional Parameters:
Allows specifying default values for parameters in the method declaration.
Example:
csharp
Copy code
public void PrintMessage(string message = "Hello") {
Console.WriteLine(message);
}
PrintMessage(); // Outputs "Hello" due to default value

4. Collections in C#

List<T>: A dynamic collection that can store elements of a specified type (T) and automatically resizes as elements are added or removed.
Key Methods and Properties:
Add: Adds an element to the end of the list.
csharp
Copy code
List<int> numbers = new List<int>();
numbers.Add(10); // Adds 10 to the list

Insert: Adds an element at a specified index, shifting other elements.
csharp
Copy code
numbers.Insert(1, 20); // Inserts 20 at index 1

Remove: Removes the first occurrence of a specified value.
csharp
Copy code
numbers.Remove(10); // Removes the first 10 from the list

Clear: Removes all elements from the list, leaving it empty.
csharp
Copy codec
numbers.Clear(); // Empties the list

Count: Retrieves the number of elements in the list.
csharp
Copy code
Console.WriteLine(numbers.Count); // Outputs the number of items

Array vs. List<T>:
Arrays have a fixed size, whereas List<T> can dynamically resize.
Lists provide more flexibility with methods like Add, Remove, and Insert.

5. Dictionary<TKey, TValue>

Definition: A collection of key-value pairs where each key is unique. Used when you need to retrieve values based on unique keys.
Key Points:
Keys must be unique, but values can be duplicated.
Commonly used methods and properties include:
Add: Adds a key-value pair.
csharp
Copy code
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Alice", 25);

Remove: Removes the key-value pair with the specified key.
ContainsKey: Checks if a specific key exists.
Count: Gets the number of key-value pairs in the dictionary.

6. Common Exam MCQ Styles

Method Syntax Errors: Questions may have small syntax errors (e.g., missing return type, incorrect use of void).
Example MCQ:
Which of the following correctly defines a method with a return type of int?
(a) void Add(int x, int y) { return x + y; }
(b) int Add(int x, int y) { return x + y; } (Correct)
(c) Add(int x, int y) { return x + y; }
(d) public Add(int x, int y)
Parameters and Return Types:
Example MCQ:
What does the ref keyword do in C#?
(a) Allows a method to receive a value and make a local copy.
(b) Prevents the method from modifying the variable.
(c) Allows the method to modify the original variable. (Correct)
(d) Specifies that the parameter cannot be null.
List and Dictionary Operations:
Be familiar with methods like Add, Insert, Remove, Clear, and properties like Count.
Example MCQ:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.