Presentation is loading. Please wait.

Presentation is loading. Please wait.

Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.

Similar presentations


Presentation on theme: "Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct."— Presentation transcript:

1 Value Types

2 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct value type –definition –use –advantages –limitations

3 3 Motivation Programs make heavy use of some data objects –local variables, parameters, loop counters, etc. Important that implementation be efficient –memory allocation –access –memory reclamation void Process() { for (int i = 0; i < 100000; i++) {... Point p = new Point();... } would be expensive to allocate and reclaim at each iteration

4 4 Runtime stack Local variables and parameters stored on runtime stack –memory allocated and reclaimed automatically –efficient since memory management overhead is low void Process(int x) { int y; Stock s;... } parameter local int local reference Process x y s... stack

5 5 Managed heap Reference type instances stored in managed heap –less efficient than stack due to overhead of heap management void Process(int x) { int y; Stock s = new Stock();... } reference type name price shares heap Process x y s... stack

6 6 Value types Value types contain data directly –not reference/object pair like reference types All value types are derived from library ValueType class –many provided: int, char, TimeSpan, etc. –can define custom: struct, enum ValueType... Object value types

7 7 Simple types Simple types are library structures in the System namespace –can use structure name or C# alias Int32 i = 4; int j; j = i; structure name C# alias boolBoolean charChar sbyteSByte byteByte shortInt16 ushortUInt16 intInt32 uintUInt32 longInt64 ulongUInt64 floatSingle doubleDouble decimalDecimal Boolean character integer floating point same type so can interoperate

8 8 Struct Use struct to define custom value type –automatically derived from ValueType ValueType struct Object programmer defined struct

9 9 Struct abilities struct has fields, constructors, properties, methods, etc. struct Point : IMeasureable { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } public void Scale(int a) { x *= a; y *= a; } public double Length() { return Math.Sqrt(x * x + y * y); } fields constructor properties method interface method

10 10 Struct limitations Struct has several limitations –does not support inheritance –cannot use variable initializers –cannot write custom default constructor –cannot define a destructor –constructors must explicitly initialize all fields struct Point3D : Point { private int z = -1; ~Point3D() {... }... } error

11 11 Value type local variable / parameter Value type local variables and parameters stored on stack –efficient to allocate and reclaim memory Process... stack void Process(Point p) { Point q = new Point();... } local variable pxypxy qxyqxy parameter

12 12 Value type field Reference type may have field of value type –field stored directly in containing object –reduces number of objects allocated in heap void Process() { Rectangle r = new Rectangle();... } reference type heap Process r... stack class Rectangle { Point ll; Point ur;... } value type fields ll x y ur x y

13 13 Value type array Array of value types contains actual data –each element is default constructed –reduces number of object allocated in heap Point[] polygon = new Point[5]; array of 5 Point s, all default constructed polygon x 0 y 0 x 0 y 0 x 0 y 0 x 0 y 0 x 0 y 0

14 14 Default constructors Value types all have default constructors that initialize fields –numeric types set to 0 –char set to null character –bool set to false –reference fields set to null Programmer cannot code default constructor –always provided by compiler int j = new int(); Point p = new Point(); j set to 0 p set to (0,0) 0 j x 0 y 0 p

15 15 Creation Create value type instance implicitly or explicitly using new Explicit creation recommended –invokes constructor so all fields will be initialized Implicit creation does not invoke constructor –fields not initialized and must be assigned to before use void Process() { Point p; Point q = new Point(); } implicit explicit x - y - p x 0 y 0 q

16 16 Value semantics Value types have value semantics –assignment –parameter passing –method return value –equality testing

17 17 Assignment Assignment performs memberwise assignment –value of each field is copied Point p = new Point(3, 4); Point q = new Point(); q = p; assign x 3 y 4 q x 3 y 4 p

18 18 Value parameter Value type can be passed by value –memory allocated for parameter –field values copied –changes made in method affect only local copy Point p = new Point(3, 4); Process(p); pass x 3 y 4 q x 3 y 4 p void Process(Point q) {... } value parameter

19 19 Reference parameter Value type can be passed ref or out –no copy made –changes made in method affect actual parameter Point p = new Point(3, 4); Process(ref p); pass x 5 y 6 p,q void Process(ref Point q) { q.X = 5; q.Y = 6; } ref parameter changes p

20 20 Method return value Value type returned from method by value –field values copied Point Add(Point a, Point b) { Point c = new Point(a.X + b.X, a.Y + b.Y); return c; } value copied out of method

21 21 Equality Equals method used to compare value types –overridden in class ValueType to compare values –no need to override unless can implement more efficiently class ValueType { public override bool Equals(object obj) {... }... } Point p = new Point(1, 2); Point q = new Point(1, 2); if (p.Equals(q))... true, same data

22 22 Boxing Value types are type compatible with object –value copied into wrapper automatically –called boxing Boxing most useful for temporary storage –values often boxed and stored in data structure –later unboxed and used Point p = new Point(3, 4); object o = p;... boxed o x 3 y 4 x 3 y 4 p

23 23 Unboxing Extract value type instance from box using cast –System.InvalidCastException thrown if cast fails void Process(object o) { Point q = (Point)o;... } unbox o x 3 y 4 x 3 y 4 q

24 24 Copy during boxing Boxing copies value into new memory location –changes to original do not affect boxed copy Point p = new Point(3, 4); object o = p; p.y = 5;... value copied into box o x 3 y 4 x 3 y 5 p modify original, boxed copy unchanged

25 25 Boxing efficiency Boxing incurs runtime overhead –memory for wrapper allocated on managed heap –value copied into box –memory garbage collected after box no longer referenced Point p = new Point(3, 4); object o = p;... boxed heap o x 3 y 4

26 26 Summary Value types contain data directly –implemented efficiently –have value semantics Value types interoperate with object –boxing occurs automatically –cast required for unboxing –use incurs some runtime overhead Special category called simple value types –provide fundamental data types Struct provides way to define structured value type


Download ppt "Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct."

Similar presentations


Ads by Google