C# Programming Examples Collection
This repository contains a series of C# programs demonstrating various core and advanced features of the language. Each program targets a specific concept, designed to help you learn and understand modern C# features and best practices.
Programs Included
- Create and Use Records with init Properties
Demonstrates immutable record types using init properties.
Shows how to create modified copies with the with expression.
- Demonstrate Inheritance and Method Overriding
Explores class inheritance and method overriding.
Base class Shape with derived classes Circle and Rectangle.
- Differentiate Abstract Classes and Interfaces
Illustrates differences between abstract classes and interfaces.
Implements polymorphism using Vehicle abstract class and IDrivable interface.
- Handle Null References Safely
Shows safe handling of nullable reference types.
Uses null-conditional (?.) and null-coalescing (??) operators.
- Use Null-Conditional Chaining in a Contact App
Demonstrates null-conditional chaining to avoid null reference exceptions.
Implements a simple contact list example.
- Use the required Modifier in C# 12
Uses the new required modifier to enforce required properties at object initialization.
- Work with Lists and Dictionaries
Basic operations with List and Dictionary<K,V>.
Adding, removing, and iterating over collections.
- Use LINQ for Filtering and Projection
Uses LINQ queries to filter and project data from a list of orders.
- Use Pattern Matching with is and switch
Demonstrates type checking and pattern matching with is and enhanced switch.
- Create and Deconstruct Tuples
Returns multiple values using tuples.
Shows tuple deconstruction.
- Simulate Async File Upload with Exception Handling
Asynchronous method simulating file upload delay.
Exception handling using try-catch.
- Serialize and Deserialize JSON Files
Serializes an object to JSON and saves to a file.
Deserializes JSON back into an object.
- Use FileStream and MemoryStream
Reads text from a file using FileStream.
Writes to a MemoryStream and reports bytes written.
- Demonstrate Race Conditions with Multi-threading
Illustrates race conditions in multi-threaded code.
Uses lock to prevent race conditions.
- Simulate and Resolve a Deadlock
Simulates a deadlock scenario with two threads.
Resolves deadlock using Monitor.TryEnter.
- Log with System.Diagnostics.Trace
Implements logging to console and file using Trace.
Configures TextWriterTraceListener.
- Sanitize Input and Prevent XSS
Simulates user input form.
Sanitizes input using HTML encoding to prevent XSS attacks.
- Perform CRUD Operations using ADO.NET
Connects to SQL Server using SqlConnection.
Performs Create, Read, Update, Delete operations on Employees table.
Requirements
.NET 6 or later (some programs use C# 9 or 12 features)
SQL Server for program 30
Visual Studio or any C# compatible IDE
How to Use
-
Clone the repository.
-
Open the solution or individual .cs files in your IDE.
-
Build and run the program you want to test.
-
Modify the code to experiment with features and concepts.
Author Saritha Chowdary