0% found this document useful (0 votes)
7 views

CSharp_Advanced_Features_and_Exception_Handling

The document outlines advanced features of C# including LINQ, async/await, delegates, nullable types, generics, indexers, reflection, and the dynamic keyword, which enhance application development. It also discusses exception handling in C#, detailing the try-catch-finally syntax, types of exceptions, custom exception handling, best practices, and exception filters introduced in C# 6.0. These features collectively improve performance, maintainability, and robustness in C# applications.

Uploaded by

Sahil Khan
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)
7 views

CSharp_Advanced_Features_and_Exception_Handling

The document outlines advanced features of C# including LINQ, async/await, delegates, nullable types, generics, indexers, reflection, and the dynamic keyword, which enhance application development. It also discusses exception handling in C#, detailing the try-catch-finally syntax, types of exceptions, custom exception handling, best practices, and exception filters introduced in C# 6.0. These features collectively improve performance, maintainability, and robustness in C# applications.

Uploaded by

Sahil Khan
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
You are on page 1/ 4

Advanced Features of C# and Exception Handling

(a) Advanced Features of C#

C# is a modern, object-oriented programming language with a rich set of advanced features that support

robust application development. These features enhance performance, maintainability, and developer

productivity.

1. LINQ (Language Integrated Query)

- Provides a consistent model for working with data across various sources (arrays, lists, databases, XML).

- Example:

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

var evenNumbers = from n in numbers where n % 2 == 0 select n;

2. Async and Await

- Used for asynchronous programming to improve application responsiveness.

- Allows non-blocking calls to I/O operations (e.g., file system, web services).

- Example:

public async Task<string> GetDataAsync()

HttpClient client = new HttpClient();

return await client.GetStringAsync("https://example.com");

3. Delegates and Events

- Delegates are type-safe method pointers, and events are based on delegates.

- Useful in designing extensible and event-driven applications.

- Example:

public delegate void Notify();

public event Notify OnCompleted;

4. Nullable Types
Advanced Features of C# and Exception Handling

- Allow value types (like int, double) to represent null values.

- Syntax: int? age = null;

5. Generics

- Enable code reuse and type safety.

- Example: List<int> list = new List<int>();

6. Indexers

- Allow objects to be indexed like arrays.

- Example:

public string this[int index] => data[index];

7. Reflection

- Enables inspection of metadata and dynamic invocation of types/methods at runtime.

- Example:

Type type = typeof(MyClass);

8. Dynamic Keyword

- Used when type resolution is deferred to runtime.

- Example:

dynamic x = 10; x = "Now a string";

(b) Exception Handling & Error Handling in C#

C# provides structured exception handling to deal with runtime errors gracefully and robustly.

1. Basic Syntax of Try-Catch-Finally

try

int result = 10 / 0;
Advanced Features of C# and Exception Handling

catch (DivideByZeroException ex)

Console.WriteLine("Cannot divide by zero.");

finally

Console.WriteLine("This block always executes.");

2. Types of Exceptions

- NullReferenceException: Accessing members on a null object.

- DivideByZeroException: Division by zero.

- FileNotFoundException: File not found during file operations.

- IndexOutOfRangeException: Invalid array index.

3. Custom Exception Handling

- Developers can define custom exception types for application-specific errors.

- Example:

public class MyCustomException : Exception

public MyCustomException(string message) : base(message) {}

4. Error Handling Best Practices

- Catch specific exceptions first.

- Always log exceptions for debugging.

- Avoid empty catch blocks.

- Use 'finally' for cleanup operations (like closing files or database connections).

- Do not use exceptions for normal program flow.


Advanced Features of C# and Exception Handling

5. Exception Filters (C# 6.0+)

- Allows conditionally catching exceptions.

- Example:

catch (Exception ex) when (ex.Message.Contains("specific"))

// Handle specific message

You might also like