.NET Framework and C# Basics
1. History and Overview of .NET Framework
-
Developed by Microsoft (2002) to provide a consistent programming model for Windows apps.
-
Supports multiple languages (C#, VB.NET, F#).
-
Runs on Common Language Runtime (CLR), ensuring language interoperability.
-
Provides a Base Class Library (BCL) for I/O, collections, networking, etc.
Key Features:
-
Cross-language interoperability (all .NET languages compile to IL).
-
Automatic memory management (Garbage Collection).
-
Security (Code Access Security, role-based security).
-
Versioning and deployment support.
-
Supports desktop, web, mobile, and cloud apps.
Diagram (link): .NET Architecture
2. Components and Versions
Main Components:
-
CLR (Common Language Runtime)
- Executes code, manages memory, type safety, exception handling, security.
-
BCL (Base Class Library)
- Pre-built classes for collections, file handling, XML, threading, etc.
-
CLS (Common Language Specification)
- Defines a set of rules all .NET languages must follow.
-
CTS (Common Type System)
- Ensures type compatibility across languages.
-
ASP.NET → Web development
-
ADO.NET / Entity Framework → Database access
-
WPF / WinForms → Desktop UI
Versions Timeline (major):
-
.NET Framework 1.0 → 2002
-
.NET Framework 2.0 → 2005
-
.NET Framework 3.5 → 2007 (LINQ, WCF, WPF)
-
.NET Framework 4.0/4.5 → 2010/2012
-
.NET Core (2016) → cross-platform
-
.NET 5 (2020) → unified platform
-
Latest → .NET 8 (2023)
Diagram (link): CLR & Components
3. Introduction to C#
3.1 C# Language
-
Object-oriented, type-safe, modern programming language by Microsoft.
-
Runs on CLR, ensuring safety and portability.
-
Syntax similar to C++ and Java.
Example:
using System;
class HelloWorld
{
static void Main()
{
Console.WriteLine("Hello, C#!");
}
}3.2 C# Language Elements
-
Namespace: Container for classes.
-
Class: Blueprint of objects.
-
Method: Function inside class.
-
Variable: Stores data.
-
Access Modifiers: public, private, protected, internal.
3.3 Data Types
Value Types
-
Store actual value.
-
Examples: int, char, bool, float, struct.
-
Stored in stack.
int x = 10;
char c = 'A';Reference Types
-
Store reference (address) to data.
-
Examples: string, object, arrays, class, interface.
-
Stored in heap.
string name = "Shivam";
object obj = name;3.4 Boxing and Unboxing
-
Boxing → Converting value type → reference type.
-
Unboxing → Converting reference type → value type.
Example:
int val = 100; // value type
object obj = val; // boxing
int num = (int)obj; // unboxing3.5 Enum and Constant
- Enum → Collection of named constants.
enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
Days today = Days.Mon;- Constant → Fixed value, cannot be changed.
const double PI = 3.14159;3.6 Operators & Control Statements
Operators:
-
Arithmetic:
+ - * / % -
Relational:
== != > < >= <= -
Logical:
&& || ! -
Assignment:
= += -=
Control Statements:
if (x > 10)
Console.WriteLine("Large");
else
Console.WriteLine("Small");
for (int i = 0; i < 5; i++)
Console.WriteLine(i);
switch (day)
{
case 1: Console.WriteLine("Mon"); break;
default: Console.WriteLine("Other day"); break;
}3.7 Working with Arrays and Strings
Array Example:
int[] arr = { 1, 2, 3, 4 };
foreach (int i in arr)
Console.WriteLine(i);String Example:
string s1 = "Hello";
string s2 = "World";
string result = s1 + " " + s2; // concatenation3.8 Pass by Value and Pass by Reference
- Pass by Value (default) → Copy of variable passed.
void Square(int n) { n = n * n; }- Pass by Reference (using ref) → Actual variable passed.
void Square(ref int n) { n = n * n; }3.9 Parameters, Variable Length Parameter
Normal Parameter:
void Add(int a, int b) { Console.WriteLine(a + b); }Optional Parameter:
void Print(string name = "Guest") { Console.WriteLine("Hello " + name); }Variable Length Parameter (params):
void Sum(params int[] nums)
{
int total = 0;
foreach (int n in nums) total += n;
Console.WriteLine("Sum: " + total);
}
Sum(1, 2, 3, 4); // Output: Sum: 10