Advanced Features of C# and Exception Handling
Operators and Expressions (16 Marks)
Operators are special symbols that perform operations on variables and values. Expressions are
combinations of variables, operators, and values that yield a result.
Types of Operators in C#:
1. Arithmetic Operators:
- Used for basic mathematical operations.
- +, -, *, /, %, ++, --
- Example: int sum = a + b;
2. Relational (Comparison) Operators:
- Used to compare values.
- ==, !=, >, <, >=, <=
- Example: if (a > b) { ... }
3. Logical Operators:
- Combine multiple conditions.
- && (AND), || (OR), ! (NOT)
- Example: if (a > 0 && b > 0) { ... }
4. Assignment Operators:
- Assign values to variables.
- =, +=, -=, *=, /=, %=
- Example: x += 5; // same as x = x + 5
5. Bitwise Operators:
- Perform operations on bits.
- &, |, ^, ~, <<, >>
Advanced Features of C# and Exception Handling
- Example: int result = a & b;
6. Conditional (Ternary) Operator:
- Short form of if-else.
- Syntax: condition ? expr1 : expr2
- Example: int result = (a > b) ? a : b;
7. Null-Coalescing Operator (??):
- Returns left operand if not null, else right operand.
- Example: string name = inputName ?? "Default";
8. Type Checking and Type Casting Operators:
- is, as, typeof, cast operators.
- Example: if (obj is string) { ... }
Expressions:
- An expression is a valid combination of literals, operators, variables, and method calls.
- Example: (a + b) * c is an arithmetic expression.
7(a) Control Constructs in C# - Decision Making (8 Marks)
Control constructs guide the flow of execution in a program.
1. if Statement:
- Executes a block if the condition is true.
- Example:
if (age >= 18) { Console.WriteLine("Adult"); }
2. if-else Statement:
- Provides alternative path.
Advanced Features of C# and Exception Handling
- Example:
if (x > y) Console.WriteLine("X is greater");
else Console.WriteLine("Y is greater");
3. else-if Ladder:
- For multiple conditions.
- Example:
if (mark >= 90) Console.WriteLine("A+");
else if (mark >= 75) Console.WriteLine("A");
else Console.WriteLine("B");
4. switch Statement:
- Replaces multiple if-else.
- Syntax:
switch (grade)
case 'A': Console.WriteLine("Excellent"); break;
case 'B': Console.WriteLine("Good"); break;
default: Console.WriteLine("Try Again"); break;
5. Conditional (ternary) Operator:
- Shorthand for if-else.
- Syntax: condition ? expr1 : expr2;
7(b) Constructors and Destructors (8 Marks)
Constructors:
- Special methods used to initialize objects.
- Same name as class, no return type.
Advanced Features of C# and Exception Handling
- Automatically invoked when object is created.
Types of Constructors:
1. Default Constructor:
- No parameters.
- Example:
class Car { public Car() { ... } }
2. Parameterized Constructor:
- Takes arguments.
- Example:
class Car { public Car(string name) { ... } }
3. Copy Constructor (custom implementation):
- Copies values from another object.
- Example:
public Car(Car c) { this.name = c.name; }
4. Static Constructor:
- Initializes static members, runs once.
- No access modifier.
- Example:
static Car() { ... }
Destructors:
- Used for cleanup when an object is destroyed.
- Syntax: ~ClassName()
- Called by garbage collector, cannot be overloaded or inherited.
- Example:
class Car { ~Car() { Console.WriteLine("Destroyed"); } }
Advanced Features of C# and Exception Handling
Note: In C#, destructors are rarely needed due to automatic memory management.