Xem mẫu

Creating Value Types and Reference Types The variables in a program are allocated memory at run time in the system. In C#, variables are referred in two ways, value type and reference type. Value type variables contain data, whereas reference type variables hold the reference to the memory location where data is stored. This chapter explains how C# manages memory for its data type variables. It also explains the implementation of value types such as structure and enumeration. This chapter describes how to implement reference types such as arrays and collections in C#. Objectives In this chapter, you will learn to: Describe memory allocation Use structures Use enumerations Implement arrays Use collections Describing Memory Allocation The memory allocated to variables is referred to in two ways, value types and reference types. All the built-in data types such as int, char, and float are value types. When you declare an int variable, the compiler generates code that allocates a block of memory to hold an integer. int Num1=50; The preceding statement assigns a value to the int type variable Num1 and the value is copied to memory. Reference types, such as classes are handled differently by the compiler. When you declare a class variable the compiler does not generate code that allocates a block of memory to hold a class. Instead it allocates a piece of memory that can potentially hold the reference to another block of memory containing the class. The memory for the class object is allocated when the new keyword is used to create an object. Value type contains data. Reference types contain address referring to a block of memory. Value types are also called direct types because they contain data. Reference types are also called indirect types because they hold the reference to the location where data is stored. To understand value type referencing, consider a scenario, where you declare a variable named Num1 as an int and assign the value 50to it. If you declare another variable Num2 as an int, and assign Num1 to Num2, Num2 will contain the same value as Num1. However, both the variables contain different copies of the value 50. If you modify the value in Num1, the value in Num2 does not change. The following code is an example of value type variables: int Num1=50; int Num2=Num1; Num1++; // declare and initialize Num1 // Num2 contains the copy of the data in Num1 // incrementing Num1 will have no effect on Num2 The following figure is a diagrammatic representation of the memory allocated to the value type variable. int Num1; Num1=50; int Num2; Num2=Num1; Num1 50 Num2 50 Memory Allocated for Value Type Variable ¤NIIT Creating Values Types andReference Types 5.3 Note All value types are created on the stack. Stack memory is organized like a stack of books piled on a rack. To understand reference types, consider a class named Car. The object Mercedes of the class Car is initialized with Ford, which is also an object of the same class. In this case both Ford and Mercedes will refer to the same object. The following code is an example of reference type variables: using System; namespace Ref_Type { class class1 { static void Main (string[] args) { Car Ford = new Car (); Ford.Model = 10; Car Mercedes = Ford; Mercedes.Display_Model (); Ford.Display_Model (); } } class Car { public int Model; public void Display_Model () { Console.WriteLine (Model); } } } 5.4 Creating Values Types and Reference Types ¤NIIT The following figure is a diagrammatic representation of the memory allocated to the reference type variable. Car Ford= new Car(); Ford.Model=10; Car Mercedes; Mercedes=Ford; Ford *** Mercedes 10 *** Memory Allocated for the Reference Type Variable Note All reference types are created on the heap. Heap memory is like books arranged next to each other in rows. We have discussed int as a value type in the preceding examples, which is a built-in data type. There are more value types like, structures and enumerations, which are user-defined data types. We also discussed class as reference type. There are more examples of reference types like arrays and collections. ¤NIIT Creating Values Types andReference Types 5.5 ... - tailieumienphi.vn
nguon tai.lieu . vn