Drill down into C # data types

Source: Internet
Author: User

Data types can be stored in two categories: value types and reference types.

1 Value types

Value types originate from the System.ValueType family, and each object of value type has a separate memory area for saving its own values.

The memory region where the value type data resides is the stack (stack), which, as long as it is modified in the code, holds the value in its region, which mainly includes the base data type and enumeration type.

For value types, different variables allocate different storage spaces, and the value of the variable is stored in the storage space, and the value of the variable is passed by the assignment operation, and changing the value of one variable does not affect the value of the other variable.

2. Reference types

Reference types originate from the System.Object family, and in C # Reference types mainly include arrays, classes, interfaces, and so on.

For reference types, the assignment is to pass a reference to the original object to another reference, which, when an array reference is assigned to another array reference, points to the same array, that is, to the same block

Storage space.

Fine-score types and reference types

Value type: 1. Basic data Type 2. Enumeration Type 3. Structure type

Reference type: 1. Class 2. Interface 3. Array

Structure

1. Definition of structure

Access modifier struct struct name

{

Structural body

}

The definition of a structure has the following characteristics:

1. There can be fields in the structure, or there can be methods

2. When defining, the fields in the structure cannot be assigned an initial value

Use of structures

1. You can not use new

2. Once you declare an object of a struct, you must assign an initial value to the members of the struct.

Packing and unpacking

1 int 123 ; 2 object o = i;      // Boxing: Value type conversion to reference type 3 int j= (int) o;     // unpacking: Converting a reference type to a value type

Different types of parameter passing

1. How do I differentiate between a value pass or a reference pass?

The basis for judging is there is no ref.

2. When passing a value type parameter using a value method (without ref decoration), the modification of the parameter in the method is not persisted.

When a reference type parameter is passed using a value method (without a ref decoration), the modification of the parameter in the method is preserved

When you pass a value type or reference type parameter by using a reference method (with ref decoration), the modification of the parameter in the method is preserved

In-depth C # data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.