1 / 20

Value Types and Reference Types Enumerations and Structures

Value Types and Reference Types Enumerations and Structures. 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

elysia
Télécharger la présentation

Value Types and Reference Types Enumerations and Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Value Types and Reference Types Enumerations and Structures

  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. Value Types • Most intrinsic data types are value types • bool, byte, decimal, double, float, int, long • They all derive from System.ValueType

  4. Value Type (Illustration)

  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. ValueType (Illustration) inti = 42; int j = 43; Program Stack i 42 j 43

  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. Reference Type (null reference) int[] i; Program Stack Heap i null

  9. Reference Type (Initialized) Program Stack Heap i 1,2,3 @ int[] i = new int[] {1,2,3};

  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. 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. Enumerations (Example 1) • The StartPosition property is really an enumeration

  13. Enumerations (Example 2) • Message box constants are really enumerations

  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. 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. 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. 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. Structures (Declaring) • Declare a struct named NameInfo with two string members public structNameInfo { public string FirstName; public string LastName; }

  19. Structures (Declaring Variables) • Declare a variable having the structure as its data type NameInfo n; n.FirstName = "Joe"; n.LastName = "Beets";

  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

More Related