CSharp Flashcards

(39 cards)

1
Q

Value Types vs Reference types

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In out ref

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Class and objects

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Struct vs Enum

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Class vs Struct

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Record

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Class vs Record

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Reflection

A

Reflection: Lets you inspect types, methods, properties, and metadata at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Attributes

A

Attributes: Let you attach metadata to classes, methods, properties, etc., which only reflection can read.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Nullable Value Type

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Nullable Reference Type

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Boxing Unboxing

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Anonymous Types

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Class vs anonymous types

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Tuples

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Anonymous types vs tuples

A

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

17
Q

Tuples vs value tuples

A

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

18
Q

Var dynamic object

A

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

19
Q

Anonymous types vs var

A

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

20
Q

Extension methods

A

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

21
Q

Anonymous functions

A

Functions without name. Used to write inline short logic for one off scenarios. It can’t be reused.

22
Q

Lambda Functions

A

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

23
Q

Delegates

A

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

24
Q

Events

A

Encapsulated delegate used in publisher sunscriber patterns. Only class that describes it can invoke hence providing security over accidental overwrites.

25
Generics
Declare type safe class interface and Delegates without specifying the actual type. It eliminates the need for boxing Unboxing. Faster hence and safer than object based code that's prone to casting exceptions. Type safety is enforced at compile time
26
What is Covariance and contravariance
They are type compatibility rules for generic interface and Delegates. It is allows flexibility is assigning base or derived types without breaking compile time safety. We use it to declare how input and output types can vary Covariance is declared by out. The type parameters occur only in output positions and consumers can only read. They can't add. Here it is safe to use a derived type in place where base type is expected. But not the other way. Eg : IEnumerable, Func Contravariance is declared using in. The type parameters occurs only on input positions. The method can only operate on it and not modify produce value. Here if a method declares that it can operate on any base type then it can be assigned to methods that expect derived type. Not the other way. Contravariance lets you use a method/interface that can handle a broader type (base) wherever a more specific type (derived) is expected. Eg: Action, Icomparer
27
What is list. Covariant or contra
Invariant . It can consume and add T
28
String vs Stringbuilder
string string is immutable. Once created, its value cannot change. When you “change” a string (str += "abc";) C# actually creates a new string object in memory. Good for small text or when the text does not change often. Examples: storing names, constants. Messages StringBuilder StringBuilder is mutable. It can change its content without creating a new object every time. Much faster for repeated changes like appending, inserting, or removing text. Found in System.Text namespace. Good for large text processing (logs, dynamic reports, loops).
29
Stack vs Heap
Stack is fast storage with push and pop following LIFO. It stores value Types and method parameters automatically allocates and frees when method ends. No GC needed Each thread gets it's own stack Heap is larger and slower storage. It allocated memory for reference types when new keyword is used. It is collected by GC when no reference exists. It is shared by threads
30
Namespaces
31
Using
32
Garbage collector
33
Using
34
Managed vs unmanaged resources
35
Interface vs abstract classes
36
Static vs regular class
37
Static vs instance members
38
Try catch finally
39
Throw vs throw ex