Drill down into C # data types

Source: Internet
Author: User
Tags ming

One: value types and reference types

1. Value type:

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

Where the memory area is called a stack (stack), it is saved in the memory area as long as it is modified in the code.

(for value types, different variables are allocated different storage spaces, and the value of the variable is stored in storage space.) Assignment operation

The value of the variable is passed, changing the value of one variable does not affect the value of the other variable)

2. Reference type:

Reference types originate from the System.Object family, and in C # Reference types mainly include arrays, classes, interfaces, and so on. The memory area where the reference type resides

A domain is called a heap.

  

Question: There is a small program to know Zhang Hao and Li Ming last year height are 170, weight 60, and this year Li Ming Zhang to 180 and 70, now to output two

A person's height and weight.

A. Wrong way of thinking and code:

  

Run results

We found that Zhang Hao and Li Ming's height and weight have changed.

B. Correct ideas and code:

Run results

Graphic principle

Two: Structure

1. Syntax:

Access modifier struct struct name

{

Structural body

}

2. Structure definition has the following characteristics

A. There can be fields in the structure and there are methods.

B. When defining, the fields of the structure cannot be assigned values

3. Considerations for Structure invocation

A. In some specific cases, you can define a struct object directly (to new as possible) without new

B. After declaring a struct object, you must assign an initial value to the members of the struct

Three: Box packing and unpacking

1. The so-called boxing is to convert a value type to a reference type and vice versa as a unpacking

Example code:

Packing:

  

Unpacking:

  

2. Note: We are here to explain the packing unpacking is mainly to let everyone understand its concept, but in fact, in the actual development we should reduce the packing and

The use of unboxing, because they are stored in different ways, the performance of the loss of a large.

Four: Different types of parameter passing

1. Value passing and reference passing

There is no ref, ref is passed for reference, no value is passed

What 2.ref letters can change

Value passing: 1 value type (does not change) 2 reference type (will change)

Reference pass: 3 ref value type (will change) 4 ref reference type (will change)

  

Drill down into 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.