Thursday, February 27, 2025

C# differences


# Concept Comparison Example
1 Abstract Class vs Interface Abstract class can have implementations; interface cannot. abstract class A {} vs interface I {}
2 Ref vs Out ref requires initialization, out does not. void Method(ref int x) vs void Method(out int x)
3 Value Type vs Reference Type Value types store data directly; reference types store memory addresses. int x = 5; vs string s = "Hello";
4 == vs Equals() == checks reference for objects, Equals() checks values. "abc" == "abc" vs "abc".Equals("abc")
5 Sealed vs Static vs Abstract Class sealed prevents inheritance, static prevents instantiation, abstract enforces method overriding. sealed class A {}, static class B {}, abstract class C {}
6 Method Overloading vs Overriding Overloading is same method with different parameters; Overriding modifies base method. void Show(int x) vs override void Show()
7 Stack vs Queue Stack: LIFO, Queue: FIFO. Stack<int> stack = new Stack<int>();
8 Stack vs Heap Stack stores value types; Heap stores reference types. int x = 10; (Stack) vs new Person(); (Heap)
9 IEnumerable vs IQueryable IEnumerable executes in-memory, IQueryable executes on the database server. IEnumerable<T> (Linq in-memory) vs IQueryable<T> (Linq-to-SQL)
10 String vs StringBuilder string is immutable, StringBuilder is mutable. string s = "Hello"; vs StringBuilder sb = new();
11 Task vs Thread Task is for async programming, Thread is for parallel execution. Task.Run(() => {...}) vs new Thread(()=> {...}).Start();
12 ReadOnly vs Const vs Static const is compile-time, readonly is runtime, static belongs to class. readonly int a;, const int b = 10;, static int c;
13 Dispose vs Finalize Dispose() is called explicitly; Finalize() is called by GC. using(obj) {...} vs ~ClassName()
14 Early Binding vs Late Binding Early binding is compile-time; Late binding is runtime. var obj = new Class(); (early) vs dynamic obj = new Class(); (late)
15 XML vs JSON XML uses tags, JSON uses key-value pairs. <Person><Name>John</Name></Person> vs {"Name": "John"}
16 Static Constructor vs Instance Constructor Static runs once per class; instance runs per object. static Example() {} vs public Example() {}
17 DataSet vs DataReader DataSet stores data in memory; DataReader reads forward-only. DataSet ds = new DataSet(); vs SqlDataReader dr = cmd.ExecuteReader();
18 Hashtable vs Dictionary Dictionary<T, T> is type-safe; Hashtable is not. Dictionary<int, string> d = new();
19 Struct vs Class struct is value type; class is reference type. struct Point {} vs class Point {}
20 Interface vs Delegate Interface defines behavior; Delegate is a method reference. interface IRun {} vs delegate void MyDelegate();
21 IS vs AS Operator is checks type compatibility; as performs safe casting. if (obj is string) vs string s = obj as string;
22 Singleton vs Static Class Singleton allows only one instance; Static class cannot be instantiated. private static Singleton instance; vs static class MyClass {}
23 Lazy Loading vs Eager Loading Lazy loads data when needed; Eager loads immediately. Lazy<T> obj = new Lazy<T>();
24 Throw vs Throw ex throw preserves stack trace, throw ex resets it. throw; vs throw ex;
25 Partial Class vs Abstract Class Partial splits a class, Abstract enforces overriding. partial class MyClass {} vs abstract class MyClass {}
26 Deep Copy vs Shallow Copy Deep Copy creates a new object, Shallow Copy copies reference. Clone() for Deep, MemberwiseClone() for Shallow
27 Boxing vs Unboxing Boxing converts value type to object, Unboxing retrieves value type from object. object obj = 10; (Boxing) vs int x = (int)obj; (Unboxing)
28 Implicit vs Explicit Conversion Implicit is automatic; Explicit requires casting. int x = 10; double d = x; vs double d = 10.5; int x = (int)d;
29 Delegate vs Event Delegate is a function pointer, Event is a restricted delegate. delegate void MyDelegate(); vs event MyDelegate MyEvent;
30 Thread vs Process Thread is lightweight; Process is heavyweight. Thread t = new Thread(); vs Process p = new Process();