rockstar - mutability Flashcards

1
Q

Immutable objects

A
  • Once created, remain unchanged throughout their lifetime
  • Data can’t be changed
  • If data is changed: Old data is discarded and new data is created
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s the difference between System.String and System.Text.StringBuilder classes?

A
  • System.String is immutable
  • System.StringBuilder is mutable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the advantage of using System.Text.StringBuilder over System.String?

A
  • System.StringBuilder is better for when there is a lot of string manipulation
  • Otherwise, new strings are made every time there is a change
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is immutability important in programming?

A
  • Mutation hides change, which create (unexpected) side effects
  • Makes code thread-safe

e.g. If another thread invokes color.set after Statement 1 but before Statement 2, the value of myColorInt won’t match the value of myColorName

int myColorInt = color.getRGB(); //Statement 1
String myColorName = color.getName(); //Statement 2

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

How can you create an immutable class in C#?

A
  • Make all fields private / readonly
  • Avoid exposing any methods that can modify the internal state
  • If properties are needed, only getters and no setters
  • Ensure that any modification operation returns a new object instead of modifying the current one
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the readonly keyword in C#?

A
  • Keyword that makes sure a field can only be assigned a value at the time of declaration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the advantages of using immutable objects in multi-threaded applications?

A
  • Immutable objects cannot change their state after creation
  • Eliminates the need for locks or synchronization mechanisms
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the potential downsides of using immutable objects?

A
  • Immutable objects create new instances when modifications are made
  • Leads to increased memory usage if not managed properly
  • Can affect performance in scenarios where object creation is frequent
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you achieve immutability for collections in C#?

A
  • Using methods like .AsReadOnly()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is object pooling, and how does it relate to immutability?

A
  • Technique where objects are reused instead of created and destroyed frequently
  • Can be used to mitigate the performance impact of creating new objects by reusing existing ones when possible
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can you explain the difference between value types and reference types in the context of immutability?

A
  • Value types like ‘structs’ are mutable because their state cannot be changed
  • Reference types like ‘classes’ are mutable by default, but can be immutable through proper design
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Immutable data types

A
  • String
  • DateTime
  • Enum
  • Int, double
  • Boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Mutable data types

A
  • List
  • Dictionary
  • Queue
  • Stack
  • StringBuilder
  • Custom classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly