Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Creating Value Types and Reference Types pdf
MIỄN PHÍ
Số trang
32
Kích thước
972.0 KB
Định dạng
PDF
Lượt xem
1395

Creating Value Types and Reference Types pdf

Nội dung xem thử

Mô tả chi tiết

Creating Value

Types and Reference

Types

Chapter 5

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#.

In this chapter, you will learn to:

 Describe memory allocation

 Use structures

 Use enumerations

 Implement arrays

 Use collections

Objectives

¤NIIT Creating Values Types and Reference Types 5.3

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 50 to 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; // declare and initialize Num1

int Num2=Num1; // Num2 contains the copy of the data in Num1

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.

Memory Allocated for Value Type Variable

Describing Memory Allocation

int Num1;

Num1=50;

int Num2;

Num2=Num1;

Num1

Num2

50

50

Tải ngay đi em, còn do dự, trời tối mất!