Value Types vs Reference types
Value Types are types that hold the actual data on its variables. The actual data resides in stack which is high performance and fast but short lived. Eg int bool struct enum
Reference types are whose variables hold Reference to actual data thats stored on heap. Heap is long lived but slower.
Eg : class array delegate events
In out ref
All 3 are ways to pass value type by reference to avoid copying to improve memory usage and performance especially in scenarios of large struct
Ref is used to share variables with method. It has to be assigned outside and can be modified by method. Use for incrementing counters
Out is used to return multiple outputs from method. It need not be assigned outside but must be assigned inside the method. Used in tryparse patterns
In is used to input a large struct but doesn’t allow method to modify it. It can only read. It must be assigned outside the method
Class and objects
Class
Conceptual definition: A class is a blueprint or template for creating objects, defining their properties, methods, and behavior.
Keyword meaning: In C#, class is a keyword used to declare a user-defined reference type.
Object
Conceptual definition: An object is an instance of a class, representing a concrete entity in memory with its own state.
Keyword meaning: In C#, object is a predefined type (System.Object) that is the ultimate base type of all types. It can hold a reference to any type.
Struct vs Enum
Struct can’t have named constants. It is mutable.
Enum is immutable and can’t have methods like struct. It is a fixed set
Both can’t inheritance
Class vs Struct
Class is a reference type stored on heap. Struct is a value type stored on stack. Both are mutable
Class supports adding constructor and inheritance. Struct can’t add constructor and can’t support inheritance.
Class is matched by reference equality and struct is matched by value equality
Record
Reference type that can be matched using value equality. Best used for data carrying objects like DTO. It has concise definition. It is essentially a class but immutable and inheritance from other record not class
Class vs Record
Class and record both are reference types but class equality is checked using reference while record checks using value equality. It checks is same parameters present with same values. Class to do that is difficult because we need to override equal() and gethashcode()
Record is better for data carrying objects like DTO
Reflection
Reflection: Lets you inspect types, methods, properties, and metadata at runtime.
Attributes
Attributes: Let you attach metadata to classes, methods, properties, etc., which only reflection can read.
Nullable Value Type
Value Types can’t hold null but with Nullable it can. It provides hasvalue property to check if there is and value to extract value.
It indicates data unknown rather than assigning 0 or -1 to indicate which is not correct in true sense
Nullable Reference Type
Reference types can hold null but Nullable reference types were introduced for compile time warnings to avoid null reference exceptions in runtime. Compiler warns to assign the reference type if there is referencing of it.
Reference types and Nullable reference type behave the same in runtime
Boxing Unboxing
Boxing is when a value type is converted into reference type. The actual data is moved from stack to heap. It happens implicitly
Unboxing is when a reference type is converted into value type. It requires explicit casting and can lead to exceptions. The data is extracted and placed on stack.
It happens while using non generic Collections
Stack is fast and short lived hence better for performance. Boxing Unboxing adds to performance overhead with unsafe casting operations
Anonymous Types
Anonymous types let you create a temporary, lightweight object without explicitly defining a class.
Mostly used for data projection in LINQ queries and quick grouping of properties.
Class vs anonymous types
Class is overkill when a quick one off grouping of properties is required. Both eventually become class and stored on heap. It is better for developer. Assign anonymous type to var.
Useful in LINQ Projection
Tuples
Tuples are way to group data without defining class or struct. They are used to return multiple values from method
Two types reference tuple and value tuples.
Reference tuples convert into class and stored on heap hence slower. They have unnamed properties and immutable
Value tuples convert into struct and stored in stack hence faster.
They have named properties and mutable
Anonymous types vs tuples
Anonymous types are local to a method and can’t be used to return multiple values from a function. They are read only. They are best used for quick one off grouping of properties.
Tuples are mutable and can be mutated and return multiple values from a method
Tuples vs value tuples
Tuples and Value Tuples are way to group properties without the boilerplate of defining class or struct. The compiler generates it behind the scenes. They are used to return multiple values from a method
Tuples are legacy and value tuples are improved fast version as tuples are reference type class stored on heap and value tuples are value type struct stored on stack.
Tuples have unnamed properties and immutable. The are matched by reference equality
Value tuples have named properties and mutable. They are matched by value equality
Var dynamic object
Var is a way to declare local variables without actual type and compiler infers. It’s easy for development. It must be assigned at declaration and another type value can’t be assigned later on
Dynamic bypasses compile time check of type. It is resolved during runtime and prone to exceptions and performance issues but good when type is unknown at runtime
Object is the base type of all types and can hold both value with boxing and reference types
Anonymous types vs var
Var has name. It is for known type but types isn’t mentioned and Compiler only infers and doesn’t generate any type. It is mutable with same type.
Anonymous types have no name. It is used to group properties and Compiler generates class underneath . It is immutable
Extension methods
It is way to add methods to existing type without modification or deriving
Requires static class with static method with first parameter this and type.
Compiler add it as static method but we call as though instance methods.
LINQ Methods are extension methods on IENUMERABLE
Anonymous functions
Functions without name. Used to write inline short logic for one off scenarios. It can’t be reused.
Lambda Functions
It is modern concise way to write anonymous functions using arrow notation. Used for short inline logic.
It can be assigned to Delegates of compatible
It can hold variables in immediate scope in its closure
Delegates
Holds reference to single or multiple signature compatible methods
Type safe function pointer
Basis for events and callbacks
The methods can be invoked through delegate
Events
Encapsulated delegate used in publisher sunscriber patterns. Only class that describes it can invoke hence providing security over accidental overwrites.