Value Types Objectives Discuss concept of value types - - PDF document

value types objectives
SMART_READER_LITE
LIVE PREVIEW

Value Types Objectives Discuss concept of value types - - PDF document

Value Types Objectives Discuss concept of value types efficiency memory management value semantics boxing unboxing simple types Introduce struct value type definition use advantages


slide-1
SLIDE 1

Value Types

slide-2
SLIDE 2

Objectives

2

  • Discuss concept of value types

– efficiency – memory management – value semantics – boxing – unboxing – simple types

  • Introduce struct value type

– definition – use – advantages – limitations

slide-3
SLIDE 3

Motivation

3

  • 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

slide-4
SLIDE 4

Runtime stack

  • Local variables and parameters stored on runtime stack

– memory allocated and reclaimed automatically – efficient since memory management overhead is low

Process x y s ...

stack

void Process(int x) { int y; Stock s; ... }

parameter local int local reference

4

slide-5
SLIDE 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

5

slide-6
SLIDE 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

6

ValueType Object ...

value types

slide-7
SLIDE 7

Simple types

  • Simple types are library structures in the System namespace

– can use structure name or C# alias

bool Boolean char Char long Int64 decimal Decimal double Double sbyte SByte byte Byte short Int16 ushort UInt16 int Int32 uint UInt32 ulong UInt64 float Single

Int32 i = 4; int j; j = i;

structure name C# alias

Boolean character integer floating point

same type so can interoperate

7

slide-8
SLIDE 8

Struct

  • Use struct to define custom value type

– automatically derived from ValueType

ValueType Object

programmer defined struct

struct

8

slide-9
SLIDE 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

9

slide-10
SLIDE 10

Struct limitations

10

  • 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 error error

slide-11
SLIDE 11

Value type local variable / parameter

  • Value type local variables and parameters stored on stack

– efficient to allocate and reclaim memory

Process

...

stack

11

void Process(Point p) { Point q = new Point(); ... }

local variable parameter

p x y q x y

slide-12
SLIDE 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

12

void Process() { Rectangle r = new Rectangle(); ... } reference type class Rectangle { Point ll; Point ur; ... } value type fields heap

Process r

...

stack

ll x y ur x y

slide-13
SLIDE 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 Points, all default constructed

polygon x 0 y 0 x 0 y 0 x 0 y 0 x 0 y 0 x 0 y 0

13

slide-14
SLIDE 14

Default constructors

14

  • 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

j int j = new int(); Point p = new Point();

j set to 0 p set to (0,0)

x 0 y 0 p

slide-15
SLIDE 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

15

void Process() { Point p; Point q = new Point(); }

implicit explicit

x - y - p x 0 y 0 q

slide-16
SLIDE 16

Value semantics

  • Value types have value semantics

– assignment – parameter passing – method return value – equality testing

16

slide-17
SLIDE 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 p x 3 y 4 q

17

slide-18
SLIDE 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

18

void Process(Point q) { ... }

value parameter

x 3 y 4 q x 3 y 4 p Point p = new Point(3, 4); Process(p);

pass

slide-19
SLIDE 19

Reference parameter

  • Value type can be passed ref or out

– no copy made – changes made in method affect actual parameter

void Process(ref Point q) { q.X = 5; q.Y = 6; }

ref parameter changes p

x 5 y 6 p,q Point p = new Point(3, 4); Process(ref p);

pass

19

slide-20
SLIDE 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

  • ut of method

20

slide-21
SLIDE 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

Point p = new Point(1, 2); Point q = new Point(1, 2); if (p.Equals(q)) ...

true, same data

class ValueType { public override bool Equals(object obj) { ... } ... } 21

slide-22
SLIDE 22

Boxing

22

  • 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

  • x 3

y 4 x 3 y 4 p Point p = new Point(3, 4);

  • bject o = p;

...

boxed

slide-23
SLIDE 23

Unboxing

  • Extract value type instance from box using cast

– System.InvalidCastException thrown if cast fails

  • x 3

y 4 void Process(object o) { Point q = (Point)o; ... }

unbox

x 3 y 4 q

23

slide-24
SLIDE 24

Copy during boxing

  • Boxing copies value into new memory location

– changes to original do not affect boxed copy

x 3 y 5 p Point p = new Point(3, 4);

  • bject o = p;

p.y = 5; ...

value copied into box

  • x 3

y 4

modify original, boxed copy unchanged

24

slide-25
SLIDE 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);

  • bject o = p;

...

boxed heap

  • x 3

y 4

25

slide-26
SLIDE 26

Summary

26

  • 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