C Sharp
| Sl.No | Topic | Key Concepts | Practical Exercise |
|---|---|---|---|
| 1 | Setting Up & First Program | Installing .NET SDK, Visual Studio Code / Visual Studio, dotnet new console, Console.WriteLine(), Understanding Main() method. |
Create “Hello World”, personalize it with your name. |
| 2 | Variables & Data Types | int, double, decimal, bool, char, string, var, declaration, initialization, constants (const). |
Build a simple program that calculates the area of a circle. |
| 3 | Operators & Expressions | Arithmetic, assignment, comparison, logical operators. Operator precedence. | Create a program to swap two numbers without a third variable. |
| 4 | Control Flow: Conditionals | if, else if, else, switch expressions (modern C# pattern matching). |
Build a simple grading system (A, B, C, D, F). |
| 5 | Control Flow: Loops | for, foreach, while, do-while, break, continue. |
Print the Fibonacci sequence up to a certain number. |
| 6 | Methods & Parameters | Defining methods, parameters (value, ref, out), return types, XML comments. |
Refactor previous exercises into methods. |
| 7 | String Manipulation | String interpolation ($""), ToString(), String.Format(), common methods (Length, ToUpper(), Substring(), Split()). |
Build a simple palindrome checker. |
| 8 | Arrays & The List<T> |
int[], string[], looping through collections. Introduction to List<T> and its methods (Add, Remove, Find). |
Create a program to find the maximum and minimum value in a list of numbers. |
| 9 | Debugging Basics | Using the debugger in VS Code/VS, breakpoints, stepping through code, watch windows. | Debug a pre-written program with intentional bugs. |
| 10 | Project 1: Console App | Number Guessing Game: The computer generates a random number, and the user has to guess it with hints (“too high”, “too low”). | Implement the game using all concepts learned so far. |
| 11 | Introduction to OOP: Classes & Objects | The class keyword, fields, creating objects with new, constructors. |
Create a Book class with title and author fields. |
| 12 | OOP: Properties & Encapsulation | Auto-properties, full properties with get/set accessors, read-only properties, encapsulation principle. |
Add properties to the Book class and validate data in the setter. |
| 13 | OOP: Methods & static |
Instance methods vs. static methods, the static keyword, Math class example. |
Add a DisplayInfo() method to the Book class. |
| 14 | OOP: Inheritance & Polymorphism | The : syntax, base keyword, method overriding (virtual, override), the object base class. |
Create an EBook class that inherits from Book and adds a FileSize property. |
| 15 | OOP: Abstraction & Interfaces | abstract classes/methods, interfaces (IEnumerable, IDisposable), defining contracts. |
Create an ILoanable interface with Borrow() and Return() methods. |
| 16 | Exception Handling | try, catch, finally, multiple catch blocks, the Exception object, throwing exceptions. |
Write code that safely parses user input to a number. |
| 17 | Advanced Collections | Dictionary<TKey, TValue>, HashSet<T>, Queue<T>, Stack<T>, choosing the right collection. |
Count the frequency of words in a paragraph using a Dictionary. |
| 18 | LINQ (Language Integrated Query) | Query syntax vs. method syntax, Where, Select, OrderBy, GroupBy, FirstOrDefault. |
Query a list of objects (e.g., Products) using LINQ. |
| 19 | Delegates, Lambdas & Events | The Func<> and Action<> delegates, lambda expressions =>, introducing events. |
Use a delegate to pass a method to filter a list. |
| 20 | Project 2: OOP Console App | Inventory Management System: Manage a list of products (add, remove, list, search). Use Classes, Lists, LINQ, and File I/O. | Build a menu-driven app to manage an inventory. |
| 21 | File I/O | The System.IO namespace, File.ReadAllText, File.WriteAllText, StreamReader, StreamWriter. |
Save and load the inventory from Project 2 to a .txt file. |
| 22 | Introduction to Databases & SQL | (Theory) What is a relational database? Basic SQL: SELECT, INSERT, UPDATE, DELETE. |
Install SQL Server LocalDB or SQLite and run basic queries. |
| 23 | Entity Framework Core I | What is an ORM? Installing EF Core NuGet packages, DbContext, DbSet, Code-First approach. | Create a BlogContext and Post model class. |
| 24 | Entity Framework Core II | Configuring connections (appsettings.json), running migrations (Add-Migration, Update-Database). |
Generate and run a migration to create the database. |
| 25 | EF Core: CRUD Operations | Creating, Reading, Updating, and Deleting records using your DbContext. | Build a simple console app to perform CRUD on your Post model. |
| 26-27 | Project 3: Database App | Student Registrar: Create a console app to manage students and courses. Use EF Core for all data operations. | Model Student and Course classes with a many-to-many relationship. |
| 28 | Modern C# Features I | record types, init-only properties, pattern matching enhancements (relational patterns). |
Refactor a Person class to be a record. |
| 29 | Modern C# Features II | Top-level statements (minimal API), global using directives, file-scoped namespaces. | Refactor your first “Hello World” program to use top-level statements. |
| 30 | Asynchronous Programming | async, await, Task<T> type, HttpClient for API calls, importance in UI and web apps. |
Write a program to asynchronously fetch data from a public API. |
What is .NET Framework?
The .NET Framework is a software development platform developed by Microsoft. It provides a controlled programming environment where software can be developed, installed, and executed on Windows-based operating systems.
It simpifies…
Application development.
Provide a consistent object-oriented programming environment.
Enable code reusability and language interoperability.
Support Windows applications, web applications, and web services.
Features of .NET Framework?
| Feature | Description |
|---|---|
| Language Interoperability | Supports multiple programming languages (C#, VB.NET, F#, etc.) and allows them to work together seamlessly. |
| Base Class Library (BCL) | Provides a rich set of reusable classes, interfaces, and APIs for I/O, data, networking, security, etc. |
| Common Language Runtime (CLR) | The execution engine of .NET; manages memory, security, exception handling, and JIT compilation. |
| Automatic Memory Management | Uses Garbage Collector (GC) to free unused objects and manage memory efficiently. |
| Security | Offers Code Access Security (CAS) and Role-Based Security (RBS) for safe execution. |
| Simplified Deployment | Supports assemblies and versioning to avoid “DLL Hell” issues during deployment. |
| Cross-language Integration | Enables integration of code written in different languages within one project. |
| Support for Web and Windows Applications | Provides frameworks like ASP.NET (web apps), Windows Forms & WPF (desktop apps). |
| Scalability and Performance | Efficient execution through JIT compiler, caching, and optimized runtime features. |
| Rich Development Tools | Works with Visual Studio IDE, offering debugging, GUI designers, and productivity tools. |
Comparison table of .NET Framework vs .NET Core
| Aspect | .NET Framework | .NET Core / .NET 5+ |
|---|---|---|
| Platform Support | Windows only | Cross-platform (Windows, Linux, macOS) |
| Deployment | Installed system-wide; updates may affect all apps | Side-by-side deployment; apps can use different versions |
| Performance | Relatively slower due to legacy architecture | High performance with optimized runtime and JIT |
| Application Types | Windows Forms, WPF, ASP.NET Web Forms, ASP.NET MVC | ASP.NET Core, Console apps, Cloud-native apps, Web APIs, Blazor |
| Modern Development | Limited support for new tech (Cloud, Containers, Microservices) | Built for modern development (Cloud, Docker, Microservices) |
| Open Source | Proprietary (not open source) | Open-source, backed by Microsoft and community |
| Future Support | Legacy – no new major updates (only security fixes) | Actively developed; future direction of .NET (Unified platform) |
| Use Cases | Enterprise legacy applications, Windows-only desktop apps | Modern cross-platform apps, web apps, cloud-native apps, mobile backends |
| Tooling | Visual Studio (Windows only) | Visual Studio, Visual Studio Code, JetBrains Rider (cross-platform) |
| Versioning | .NET Framework 4.8 is the last version | .NET 5+ is continuously updated and unified |
Comparison table c# and Java
| Aspect | C# | Java |
|---|---|---|
| Platform Independent | Yes | Yes |
| Hardware Independent | Yes | Yes |
| Pure OOPs | Yes | Yes |
| Open Source | Yes (GitHub) | Yes |
| Exception Handling | Yes | Yes |
| Multithreadiing | Yes | Yes |
| Library -API | Namespaces | Packages |
| Intranet Applications / Distributed Apps | .Net Remoting | RMI |
| Collection API | System.Collection | java.util |
| Versioning | .NET Framework 4.8 is the last version | .NET 5+ is continuously updated and unified |
| CUI | Yes | Yes |
| GUI | Windows, WPF | AWT, Swings, JFX |
| Database | ADO.Net | JDBC |
| Web Applications | ASP.Net, ASP.Net Core | Servlets, JSP |
| Client Side scripting | Blazor | JavaScript |
| Mobile Applications | C# with Xamarin | Android with Java |
| ORM Implementation | EF, EF.Net | JPA – Hibernate |
| Machine Learning | ML.Net | – |
| Cloud Applications | Azure | Quarkus, Micronaut, Spring, Spring boot |
What Main( ) in C# ?
The Main method is the entry point of a C# application. It is the first method that is executed when you run your program. The operating system calls this method to start your application.
Signature (The Common Forms)
// Takes no arguments, returns nothing.
static void Main()
{
// Your code here
}
// Takes command-line arguments as an array of strings, returns nothing.
// This is the most commonly used form.
static void Main(string[] args)
{
// Your code here. 'args' contains parameters passed from the command line.
// Example: > myapp.exe hello world
// Then: args[0] is "hello", args[1] is "world"
}
// Can also return an integer (typically used for error codes).
// 0 usually means success; non-zero means an error.
static int Main(string[] args)
{
// Your code here
return 0; // Success
}
Key Characteristics
static: It belongs to the class itself, not to a specific object instance. This allows the runtime to call it without creating an object first.Naming: It must always be capitalized as
Main(notmainlike in Java/C++).Accessibility: It is typically
private(the default if no access modifier is specified) but can bepublic. The runtime can call it either way.
Identifiers in C#
Identifiers in C#
Identifiers are the names given to various programming elements in C# such as variables, methods, classes, namespaces, arrays, etc. They help the compiler and the programmer uniquely identify each item in a program.
Rules for Identifiers in C#:
Identifiers are the names given to various programming elements in C# such as variables, methods, classes, namespaces, arrays, etc. They help the compiler and the programmer uniquely identify each item in a program.
Must begin with a letter, underscore (_), or @ symbol.
Cannot start with a digit.
Can contain letters, digits, and underscores (_).
No special characters (like %, $, #) allowed.
Case-sensitive.
Example:
ageandAgeare two different identifiers.
Cannot be a C# keyword.
Example:
int,class,staticcannot be used as identifiers.
No spaces allowed.
Use camelCase or PascalCase instead.
Length can be large, but should be meaningful.
Valid and Invalid Examples:
| Valid Identifiers | Invalid Identifiers |
|---|---|
studentName |
123name (starts with digit) |
_totalMarks |
total-marks (hyphen not allowed) |
Age |
float (keyword) |
employee_salary |
emp salary (spaces not allowed) |
@class (keyword with @ prefix) |
#score (special character) |
Special Case: @ Identifiers:
C# allows using keywords as identifiers if prefixed with @. For example,
int @int = 5; // valid, even though “int” is a keyword
Console.WriteLine(@int);
Best Practices for Identifiers:
Use meaningful names (e.g.,
studentAgeinstead ofx).Follow naming conventions:
camelCase for variables & methods →
studentNamePascalCase for classes & methods →
StudentDetailsALL_CAPS for constants →
PI_VALUE
Keywords in C#
Keywords in C#
Keywords are reserved words in C#. They have predefined meanings in the language and cannot be used as identifiers (variable names, class names, etc.).
C# Keywords List:
| Category | Keywords |
|---|---|
| Data Types | bool, byte, sbyte, char, decimal, double, float, int, uint, long, ulong, short, ushort, object, string, void |
| Control Flow | if, else, switch, case, default, while, do, for, foreach, in, break, continue, goto, return, yield |
| Modifiers | public, private, protected, internal, static, readonly, sealed, const, volatile, abstract, virtual, override, unsafe, extern, partial, new |
| Class & Object Related | class, struct, interface, enum, namespace, delegate, event |
| Inheritance & Polymorphism | base, this, new, override, abstract, sealed |
| Exception Handling | try, catch, finally, throw |
| Access & Reference | ref, out, in, params, typeof, sizeof, stackalloc, fixed |
| Operators & Expressions | is, as, typeof, checked, unchecked |
| Contextual Keywords (used in special contexts) | var, dynamic, async, await, nameof |
| Others | true, false, null, using, lock |
Datatypes in C#
Data Types in C#
A data type in C# defines:
-
The type of data a variable can store (integer, floating-point, text, etc.).
-
The size in memory and the range of values it can hold.
-
The operations that can be performed on the data.
Categories of Data Types in C#:
C# data types are broadly divided into three categories:
- Value Types
- Reference Types
- Pointer Types
Value Types:
Store data directly in memory.
Examples: numbers, characters, boolean.
Stored in stack memory.
Includes:
Integral types:
int,short,long,byte,sbyte,ushort,uint,ulongFloating-point types:
float,double,decimalOther types:
char,bool,enum,struct
Reference Types:
Store a reference (address) to the data, not the actual data.
Stored in heap memory.
Includes:
stringobject(base type of all data types)class,interface,delegate,array
Pointer Types:
Store the memory address of another variable.
Used in unsafe code only.
Data Types in C# with Sizes and Ranges:
| Data Type | Size | Range |
|---|---|---|
| byte | 8-bit | 0 to 255 |
| sbyte | 8-bit | -128 to 127 |
| short | 16-bit | -32,768 to 32,767 |
| ushort | 16-bit | 0 to 65,535 |
| int | 32-bit | -2,147,483,648 to 2,147,483,647 |
| uint | 32-bit | 0 to 4,294,967,295 |
| long | 64-bit | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| ulong | 64-bit | 0 to 18,446,744,073,709,551,615 |
| float | 32-bit | ~±1.5e−45 to ±3.4e38 (7 digits precision) |
| double | 64-bit | ~±5.0e−324 to ±1.7e308 (15-16 digits precision) |
| decimal | 128-bit | ±1.0e−28 to ±7.9e28 (28-29 digits precision) |
| char | 16-bit (Unicode) | Single character (e.g., ‘A’) |
| bool | 1-bit (stored as 1 byte) | true or false |
| string | (Reference type) | Sequence of characters |
| object | (Reference type) | Base type of all data types |
Operators
An operator in C# is a symbol that tells the compiler to perform a specific operation on one or more operands (variables, constants, values).
Types of Operators
C# provides several categories of operators:
- Arithmetic Operators
- Relational (Comparison) Operators
- Conditional (Ternary) Operator
- Logical Operators
- Assignment Operators
- Unary Operators
- Bitwise Operators
- Type Operators
Arithmetic Operators:
Used for basic mathematical operations
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Relational (Comparison) Operators
Used to compare values; result is true or false.
| Operator | Description | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Conditional (Ternary) Operator
It is shorthand for if-else condition.
Syntax:
Logical Operators
Used with boolean values.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | (10 > 5 && 7 < 10) |
| || | Logical OR | (10 > 5 || 7 > 10) |
! | Logical NOT | !(a > b) |
Assignment Operators
Used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
= | Assign | a = 10 |
+= | Add and assign | a += 5 (same as a = a + 5) |
-= | Subtract and assign | a -= 5 |
*= | Multiply and assign | a *= 5 |
/= | Divide and assign | a /= 5 |
%= | Modulus and assign | a %= 5 |
Unary Operators
Work on a single operand.
| Operator | Description | Example |
|---|---|---|
+ | Unary plus (no change) | +a |
- | Unary minus (negation) | -a |
++ | Increment (prefix or postfix) | a++, ++a |
-- | Decrement (prefix or postfix) | a--, --a |
! | Logical NOT | !true → false |
Bitwise Operators
Operate on bits.
| Operator | Description | Example |
|---|---|---|
& | Bitwise AND | a & b |
| ` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT (complement) | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
Type Operators
Used for type checking and casting.
| Operator | Description | Example |
|---|---|---|
is | Checks if object is a certain type | if (x is int) |
as | Safe type casting | obj as string |
typeof | Returns type of class | typeof(int) |
sizeof | Returns size of data type | sizeof(int) |
checked | Checks overflow at runtime | checked { int x = int.MaxValue + 1; } |
unchecked | Ignores overflow | unchecked { int x = int.MaxValue + 1; } |
What is Value type
Value Types store their data directly in memory where the variable is allocated. When you assign one value type to another, a copy of the value is created.
