C# is a multi paradigm programming language which has,
- strong typing (error if argument pass to a function does not match expected type. See strong typing vs static typing)
- imperative (statements which changes the state in contrast to functional programming- see diff)
- declarative
- functional
- generic
- object oriented and
- component oriented
Data Types
Value type
Directly contain data in its own memory location
Each instance has its own copy of data
Value type can be divided into Simple, Enum, Struct and Nulllable types
http://stackoverflow.com/questions/1769306/why-are-net-value-types-sealed
http://stackoverflow.com/questions/1769306/why-are-net-value-types-sealed
Reference type
Stores a reference o the data (object)
Can be divided into Class, Interface, Array and Delegate types
It is not exactly true that value types goes on stack and reference types goes on the heap
Notes
A variable is an association between a name and a slot of memory. Variable has a value which the content of memory slot is associated with. The size depends on the type of the variable.
Value of a reference type is always a reference or null. If it's a reference it should be compatible with type of the variable. The slot of memory associated with the variable is size of the reference (Int32 : 4 bytes).
Value of value type is always data for an instance of the type itself. The slot of memory is large enough to hold its value (struct with 2 integers). Value type cannot have null. Null means variable of a reference type does not point to anything.
local variables are stored on stack, including reference type variables.
http://jonskeet.uk/csharp/memory.html
A variable is an association between a name and a slot of memory. Variable has a value which the content of memory slot is associated with. The size depends on the type of the variable.
Value of a reference type is always a reference or null. If it's a reference it should be compatible with type of the variable. The slot of memory associated with the variable is size of the reference (Int32 : 4 bytes).
Value of value type is always data for an instance of the type itself. The slot of memory is large enough to hold its value (struct with 2 integers). Value type cannot have null. Null means variable of a reference type does not point to anything.
local variables are stored on stack, including reference type variables.
http://jonskeet.uk/csharp/memory.html
Mutable vs. Immutable
- Mutable : can change, Immutable : Cannot change
- A mutable string can be changed, immutable string cannot
- Immutable type is a class or struct written so that it cannot be changed once it is created.
- Once a string is constructed, it cannot be changed. All the functions you can call on a string return a new string, instead of manipulating the string they are called on
- Instances of immutable types are inherently thread-safe (No thread can modify it)
- source , mutable structs are evil ,
- Always make value types Mutable (Eric Lappert)
0 comments:
Post a Comment