0% found this document useful (0 votes)
2 views3 pages

CSharp DotNet Complete Guide

.NET is a free, cross-platform developer platform from Microsoft for building various types of applications, with C# being the primary language used for .NET development. The document covers fundamental concepts of C# including data types, control statements, object-oriented programming, collections, exception handling, and advanced topics like LINQ and microservices. It also includes practical examples and a mini project for a Student Management system to illustrate the application of these concepts.

Uploaded by

kchandana2626
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)
2 views3 pages

CSharp DotNet Complete Guide

.NET is a free, cross-platform developer platform from Microsoft for building various types of applications, with C# being the primary language used for .NET development. The document covers fundamental concepts of C# including data types, control statements, object-oriented programming, collections, exception handling, and advanced topics like LINQ and microservices. It also includes practical examples and a mini project for a Student Management system to illustrate the application of these concepts.

Uploaded by

kchandana2626
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

Chapter 1: Introduction to .

NET Core and C#

What is .NET? .NET is a free, cross-platform, open-source developer platform from Microsoft for building
apps of all kinds: web, mobile, desktop, cloud, gaming, IoT, and AI.
.NET Core (now unified into .NET 5, 6, 7...) is the modern, cross-platform version that runs on Windows,
Linux, and macOS.
Why C#? C# is the most widely used language for .NET. It is type-safe, object-oriented, and designed for
productivity with features like LINQ, async/await, pattern matching, and more.
Real-world example: A banking application backend API can be written in .NET Core and deployed in Docker
containers across Linux servers, while the same C# code can be reused in desktop tools for admins.
First C# Program.
// Hello World in C#
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, .NET Core World!");
}
}

Chapter 2: Data Types, Variables and Operators

Data Types: C# has value types (int, double, bool, struct) stored on stack, and reference types (class, array,
string) stored on heap.
Operators: arithmetic (+, -, *, /, %), comparison (==, !=, <, >), logical (&&, ||, !), bitwise (&, |, ^, ~), and
assignment (=, +=, -=).
Real-world example: In payroll software, int can represent employee ID, decimal can represent salary
(precise for money).
Boxing/unboxing: assigning a value type to object (boxing) and retrieving back (unboxing). Example: object o
= 10; int x = (int)o;
Variables Example.
int id = 101;
decimal salary = 35000.75m;
bool isActive = true;
Console.WriteLine($"ID:{id}, Salary:{salary}, Active:{isActive}");
Boxing and unboxing.
object o = 25; int x = (int)o; Console.WriteLine(x);

Chapter 3: Control Statements in C#

Decision making: if, if-else, switch.


Loops: for, while, do-while, foreach (used with collections).
Jump statements: break, continue, return, goto (rarely used).
Real-world example: ATM software loops until user exits menu.
For loop example.
for(int i=1; i<=5; i++) Console.WriteLine(i);
Switch statement.
string role = "admin";
switch(role) {
case "admin": Console.WriteLine("Welcome Admin"); break;
case "user": Console.WriteLine("Welcome User"); break;
default: Console.WriteLine("Unknown role"); break;
}

Chapter 4: Object-Oriented Programming in C#

Encapsulation: binding fields & methods inside a class with controlled access (public/private/protected).
Inheritance: creating new classes from existing ones. Supports reusability. Use 'virtual' and 'override' for
polymorphism.
Polymorphism: ability to process objects differently based on their data type (method overriding, interfaces).
Abstraction: hiding implementation using abstract classes and interfaces.
Sealed classes prevent inheritance; Partial classes split definitions across files.
Polymorphism Example.
class Animal {
public virtual void Speak(){ Console.WriteLine("Animal sound"); }
}
class Dog: Animal {
public override void Speak(){ Console.WriteLine("Woof"); }
}
// Usage
Animal a = new Dog(); a.Speak();

Chapter 5: Collections and Generics

Collections are containers for groups of objects.


List<T>: dynamic array, Dictionary<TKey,TValue>: key-value pairs, Queue<T>: FIFO, Stack<T>: LIFO.
Generics allow type-safe reusable classes and methods.
Real-world example: E-commerce shopping cart can be a List<Item>. A product catalog is stored in a
Dictionary<Id,Product>.
List<T> Example.
var list = new List<string>{"Apple","Banana","Cherry"};
foreach(var fruit in list) Console.WriteLine(fruit);
Dictionary Example.
var prices = new Dictionary<string,decimal>{{"Pen",10.5m},{"Book",99m}};
Console.WriteLine(prices["Book"]);

Chapter 6: Exception Handling and Debugging

Exception handling ensures programs don't crash on unexpected input. Use try-catch-finally.
Always catch specific exceptions like FormatException before catching general Exception.
Use Debugger in Visual Studio for breakpoints, watches, and stepping through code.
Real-world example: Input validation in billing system using TryParse instead of Parse to avoid crashes.
Try-Catch Example.
try {
int n = int.Parse("abc");
} catch(FormatException ex) {
Console.WriteLine("Invalid input: "+ex.Message);
}

Chapter 7: Advanced Topics

Delegates: type-safe function pointers. Useful for callbacks/events.


Events: built on delegates, follow publish-subscribe model.
LINQ: Language Integrated Query for collections (like SQL for objects).
Async and Await: simplify multithreading and asynchronous programming.
Real-world example: Web API calls using HttpClient are awaited for responsiveness.
Delegate example.
// Delegate example
delegate void Printer(string msg);
class Test { static void Main(){ Printer p = Console.WriteLine; p("Hello via delegate"); }}
LINQ Example.
// LINQ Example
var nums = new List<int>{1,2,3,4,5};
var evens = from n in nums where n%2==0 select n;
foreach(var e in evens) Console.WriteLine(e);

Chapter 8: Deployment and Microservices

Publishing .NET apps: Use 'dotnet publish' command. Host in IIS (Windows), Nginx (Linux), or Docker
containers.
Microservices: breaking application into independent services. Communicate via REST/gRPC.
Real-world example: Netflix microservices architecture; in .NET Core, one service might handle billing,
another inventory, another authentication.
Create a simple Web API project.
dotnet new webapi -o MyApi
cd MyApi
dotnet run

Chapter 9: Mini Project Example

Let's build a simple console-based Student Management system.


Features: Add student, List students, Find by ID.
Mini Project: Student Management Console App.
using System;
using System.Collections.Generic;
class Student { public int Id {get;set;} public string Name{get;set;} }
class Program {
static List<Student> students = new List<Student>();
static void Main(){
while(true){
Console.WriteLine("1.Add 2.List 3.Find 4.Exit");
string choice = Console.ReadLine();
if(choice=="1"){ Console.Write("Enter ID:"); int id=int.Parse(Console.ReadLine());
Console.Write("Enter Name:"); string name=Console.ReadLine(); students.Add(new
Student{Id=id,Name=name}); }
else if(choice=="2"){ foreach(var s in students) Console.WriteLine($"{s.Id}:{s.Name}"); }
else if(choice=="3"){ Console.Write("Enter ID:"); int id=int.Parse(Console.ReadLine()); var
s=students.Find(x=>x.Id==id); if(s!=null) Console.WriteLine($"Found: {s.Name}"); else
Console.WriteLine("Not Found"); }
else if(choice=="4") break;
}
}
}

You might also like