Download presentation
Presentation is loading. Please wait.
1
Value Types and Reference Types Enumerations and Structures
2
Slide 2 Introduction to Value types and Reference types Simply put Reference types store data in the memory address of the variable itself Reference types store a 32 bit (or 64 bit) pointer to another object The variable references the object, in other words Value types are stored in the program stack
3
Slide 3 Value Types Most intrinsic data types are value types bool, byte, decimal, double, float, int, long They all derive from System.ValueType
4
Slide 4 Value Type (Illustration)
5
Slide 5 Value Type (Example) Note that Int32 type has constant members named minValue and MaxValue txtMinValue.Text = System.Int32.MinValue.ToString(); txtMaxValue.Text = System.Int32.MaxValue.ToString();
6
Slide 6 ValueType (Illustration) int i = 42; int j = 43; i j 42 43 Program Stack
7
Slide 7 Reference types (Introduction) Reference types do not store data They store a memory address. That memory address, in turn, contains the data Data (memory) is obtained from the managed heap Arrays are reference types Most everything else is too (text boxes, buttons, etc…)
8
Slide 8 Reference Type (null reference) int[] i; i Program Stack null Heap
9
Slide 9 Reference Type (Initialized) i Program Stack @ Heap int[] i = new int[] {1,2,3}; 1,2,3
10
Slide 10 Reference Type (Assignment) Both “i” and “j” point to the same array int[] i = new int[] { 1, 2, 3, 4, 5, 6, 7 }; int[] j; i[0] = 3; j = i;
11
Slide 11 Enumerations (Introduction) Simply put, enumerations give mnemonic names to integral values Their only real purpose is to improve code readability Use the enum statement to declare an enumeration Enumerations are value types
12
Slide 12 Enumerations (Example 1) The StartPosition property is really an enumeration
13
Slide 13 Enumerations (Example 2) Message box constants are really enumerations
14
Slide 14 Enumerations (Declaring) Declare an enum named Season with 4 possible values enum Season {Spring, Summer, Fall, Winter) Note that the underlying values are 0-based integers Spring = 0 Summer = 1 …
15
Slide 15 Enumerations (Using) Declare a variable named CurrentSeason having a data type of Seasons public Seasons CurrentSeason; Use the variable in an assignment statement CurrentSeason = Seasons.Spring;
16
Slide 16 Enumerations (Converting) The following prints the enumeration name txtDemo.Text = CurrentSeason.ToString(); The following prints the enumeration value int x = (int) CurrentSeason; txtDemo.Text = x.ToString();
17
Slide 17 Structures Structures allow us to logically group data a name (first, middle, last) A coordinate (x, y, z) Structures are value types Declare with the struct statement
18
Slide 18 Structures (Declaring) Declare a struct named NameInfo with two string members public struct NameInfo { public string FirstName; public string LastName; }
19
Slide 19 Structures (Declaring Variables) Declare a variable having the structure as its data type NameInfo n; n.FirstName = "Joe"; n.LastName = "Beets";
20
Slide 20 Structures (Assigning) As long as the left and right hand side of an expression have the same data type, assignment is legal. See btnStructureDemo2 in form1.cs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.