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. | 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 . Objectives In this chapter you will learn to fl Describe memory allocation fl Use structures fl Use enumerations fl Implement arrays fl 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 Numi 50 The preceding statement assigns a value to the int type variable Numi 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 Numi as an int and assign the value 5 0 to it. If you .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
2    1389    2    15-05-2024
55    324    1    15-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.