intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Value Types

Chia sẻ: Lavie Lavie | Ngày: | Loại File: PDF | Số trang:0

30
lượt xem
1
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Value Type's Objectives is Discuss concept of value types (efficiency, memory management, value semantics, boxing, unboxing, simple types); introduce structvalue type (definition, use, advantages, limitations).

Chủ đề:
Lưu

Nội dung Text: Value Types

  1. Value Types
  2. Objectives • Discuss concept of value types – efficiency – memory management – value semantics – boxing – unboxing – simple types • Introduce struct value type – definition – use – advantages – limitations 2
  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++) { would be expensive to ... allocate and reclaim Point p = new Point(); at each iteration ... } } 3
  4. Runtime stack • Local variables and parameters stored on runtime stack – memory allocated and reclaimed automatically – efficient since memory management overhead is low stack parameter void Process(int x) { local int int y; local reference Stock s; Process x y ... s } ... 4
  5. Managed heap • Reference type instances stored in managed heap – less efficient than stack due to overhead of heap management stack heap void Process(int x) { int y; reference type Stock s = new Stock(); Process x y name ... s price } ... shares 5
  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 Object ValueType value types ... 6
  7. Simple types • Simple types are library structures in the System namespace – can use structure name or C# alias Boolean bool Boolean character char Char sbyte SByte byte Byte structure name Int32 i = 4; short Int16 C# alias int j; ushort UInt16 integer int Int32 same type so can interoperate j = i; uint UInt32 long Int64 ulong UInt64 float Single floating point double Double decimal Decimal 7
  8. Struct • Use struct to define custom value type – automatically derived from ValueType Object ValueType programmer struct defined struct 8
  9. Struct abilities • struct has fields, constructors, properties, methods, etc. struct Point : IMeasureable { fields private int x, y; constructor public Point(int x, int y) { this.x = x; this.y = y; } public int X { get { return x; } set { x = value; } } properties public int Y { get { return y; } set { y = value; } } method public void Scale(int a) { x *= a; y *= a; } interface method public double Length() { return Math.Sqrt(x * x + y * y); } } 9
  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 error struct Point3D : Point { error private int z = -1; error ~Point3D() { ... } ... } 10
  11. Value type local variable / parameter • Value type local variables and parameters stored on stack – efficient to allocate and reclaim memory stack parameter void Process(Point p) { local variable Point q = new Point(); Process ... p x } y q x y ... 11
  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 reference type class Rectangle { Point ll; value type fields stack heap Point ur; ... } ll void Process() x Process y { r Rectangle r = new Rectangle(); ur x ... y ... } 12
  13. Value type array • Array of value types contains actual data – each element is default constructed – reduces number of object allocated in heap array of 5 Points, Point[] polygon = new Point[5]; all default constructed x 0 x 0 x 0 x 0 x 0 polygon y 0 y 0 y 0 y 0 y 0 13
  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 j set to 0 int j = new int(); j 0 p set to (0,0) Point p = new Point(); x 0 p y 0 14
  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() x - { p y - implicit Point p; x 0 explicit Point q = new Point(); q y 0 } 15
  16. Value semantics • Value types have value semantics – assignment – parameter passing – method return value – equality testing 16
  17. Assignment • Assignment performs memberwise assignment – value of each field is copied Point p = new Point(3, 4); Point q = new Point(); assign q = p; x 3 x 3 q p y 4 y 4 17
  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 value parameter void Process(Point q) x 3 { q y 4 ... } Point p = new Point(3, 4); x 3 p pass Process(p); y 4 18
  19. Reference parameter • Value type can be passed ref or out – no copy made – changes made in method affect actual parameter ref parameter void Process(ref Point q) { q.X = 5; changes p q.Y = 6; } p,q Point p = new Point(3, 4); x 5 pass Process(ref p); y 6 19
  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); value copied return c; out of method } 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2