Types Base
- C# has a Unified Type System in which every type derives from
System.Object
class, directly or indirectly.
- Most types do derive from
object
, including: classes, value types, and delegates.
- These types that do not derive from
object
:
- interface: an interface can be implemented by some type that does derive from
object
.
- dynamic: a dynamic type it is not an actual type in the CLR that derives from
object
.
System.TypedReference
: does not derive from object.
- The value type’s memory area points to the stack.
- For local variable value types, memory is allocated on the stack, so the GC does not need to handle the value type’s cleanup, and no finalizer is called before the stack is unwound.
- For reference types, the data is stored on the heap, and memory is cleaned up as part of the reference object’s GC.
- Value Type variable refers to the location in memory where the value is stored.
- When a different variable is assigned the same value, a memory copy of the original variable’s value is made to the location of the new variable.
- Value types keep their copy of the data. Any operation on one (any assignments) does not affect others.
- Value types in .NET: simple types, enums, structs, and nullable value types.
- The exception is in the case of
ref
and out
parameter variables.
- All the predefined types except
string
are value types.
- Passing a value type to a method will also result in a memory copy.
- Any changes to the parameter inside a method will not affect the original value within the calling function.
- Value types consume less heap memory (less than 16 bytes) because they are smaller and more easily compacted.
- They take longer to copy than a reference copy.
- All value types are sealed and derive from
System.ValueType
which has derived from object.
- Value types can implement interfaces, but cannot inherit other value types.
Simple Types
- Unicode Characters:
char
- Boolean:
bool
Signed Integral