Value Types
lượt xem 1
download
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).
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Value Types
- Value Types
- Objectives • Discuss concept of value types – efficiency – memory management – value semantics – boxing – unboxing – simple types • Introduce struct value type – definition – use – advantages – limitations 2
- 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
- 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
- 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
- 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
- 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
- Struct • Use struct to define custom value type – automatically derived from ValueType Object ValueType programmer struct defined struct 8
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- Value semantics • Value types have value semantics – assignment – parameter passing – method return value – equality testing 16
- 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
- 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
- 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
- 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
CÓ THỂ BẠN MUỐN DOWNLOAD
-
GIỚI THIỆU C# (tt)
38 p | 181 | 53
-
CSharp_Week 3: Types_Stream
38 p | 117 | 42
-
Functional testing
89 p | 148 | 32
-
C# Căn Bản Toàn Tập part 3
5 p | 142 | 21
-
Chapter 8 Operator Overloading, Friends, and References
46 p | 50 | 7
-
Lập trình C# - Phần 3: Giới thiệu về lớp (THPT Chuyên Lê Hồng Phong)
17 p | 67 | 7
-
Using the Get* Methods to Read Column Values
3 p | 120 | 6
-
Replacing Null Values in a Strongly Typed DataSet
4 p | 96 | 6
-
Chương 2: Core C# Programming Construct
65 p | 95 | 6
-
Dữ liệu kiểu trị và kiểu qui chiếu
9 p | 69 | 6
-
Chapter 4 Parameters and Overloading
41 p | 50 | 5
-
PL/SQL
68 p | 62 | 5
-
Module 7 More Data Types and Operators
36 p | 128 | 5
-
Reading a Column Value Using Strongly Typed DataSet Classes
7 p | 76 | 5
-
Strength Modeling And Advanced Net Types Definitions
5 p | 49 | 3
-
Returning Strongly Typed Column Values
2 p | 85 | 3
-
Lecture Java programming language: Variables and constants - Ho Dac Hung
15 p | 38 | 1
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn