.NET / ASP.NET Core
165 challenges · 0 mastered
C# is an object oriented programming language developed by Microsoft mainly used for building web applications, APIs, desktop applications, and backend services.
string name = "Manav";C# is the main language we use to write .NET applications.
CLR stands for Common Language Runtime. It executes .NET code and handles memory management, exception handling, and garbage collection.
// CLR executes compiled IL codeCLR is like the engine that runs .NET code.
Value types store actual data directly while reference types store memory address pointing to actual object.
int a = 5; string name = "test";int stores value directly, object stores reference.
var type is decided at compile time while dynamic type is decided at runtime.
var x = 10; dynamic y = 10;var is fixed after compile, dynamic can change while running.
Boxing converts value type to object type and unboxing converts object back to value type.
int a = 10; object obj = a; int b = (int)obj;Wrapping int inside object is boxing.
Method overloading means creating multiple methods with same name but different parameters.
Add(int a,int b)
Add(int a,int b,int c)Same method name but different input.
Method overriding means changing parent class method behavior in child class using virtual and override keywords.
public override void Show(){}Child class changes parent method behavior.
Encapsulation means hiding internal data and exposing only required functionality using properties and methods.
private int age; public int Age { get; set; }Hide data and allow controlled access.
Inheritance allows child class to reuse properties and methods from parent class.
class Admin : User {}Child class gets parent functionality.
Polymorphism allows same method to behave differently depending on object or parameters.
method overloading and method overridingSame method, different behavior.
Abstraction means hiding implementation details and showing only important functionality.
abstract class Payment {}User sees what action does, not internal logic.
Abstract class can have implementation and constructor while interface only defines contract.
interface IUserService {}Interface defines what to do, abstract can partially implement.
Yes a class can implement multiple interfaces in C#.
class User : ILogin, IRegister {}One class can follow multiple contracts.
Array has fixed size while List is dynamic and can grow or shrink.
int[] arr = new int[5]; List<int> list = new();Array fixed, List flexible.
Dictionary stores data as key value pairs and allows fast lookup using key.
Dictionary<int,string> users = new();Store like ID and Name together.
IEnumerable works in memory while IQueryable executes query on database side.
db.Users.Where(x => x.Id > 1)IQueryable filters in DB, IEnumerable filters after data comes.
Queue follows FIFO structure meaning first in first out.
Queue<int> q = new();First inserted item comes out first.
Stack follows LIFO structure meaning last in first out.
Stack<int> stack = new();Last inserted item comes out first.
Exception handling allows application to handle runtime errors without crashing.
try { } catch(Exception ex) { }Handle errors safely.
Try contains risky code, catch handles error, finally always executes.
try{} catch{} finally{}Finally runs whether error happens or not.
throw keeps original stack trace while throw ex resets stack trace.
throw; vs throw ex;Use throw to preserve original error details.
Finally is used for cleanup like closing DB connection or releasing resources.
finally { connection.Close(); }Always runs at end.
Create class inheriting Exception class.
class MyException : Exception {}Create your own custom error.
Delegate is a type safe function pointer that holds reference to methods.
delegate void Print();Delegate points to a method.
Event allows one class to notify other classes when something happens.
public event EventHandler OnSave;Button click triggers event.
Delegate can directly call methods while event is used for notifications and restricts direct invocation outside class.
event EventHandler Submit;Event is safer wrapper around delegate.
Action is built in delegate that returns no value.
Action<string> print;Method runs but returns nothing.
Func is built in delegate that returns a value.
Func<int,int,int> add;Method takes input and returns result.
