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)

Example code:

Class program{

static void Main (string[] args)

{

int Heightzhang = 170;

int heightli= Heightzhang;

Console.WriteLine ("Last year-Zhang Hao's height is:" +heightzhang+ ", Li Ming height is:" + Heightli);

Heightli = 180;

Console.WriteLine ("This year-Zhang Hao's height is:" +heightzhang+ ", Li Ming height is:" + Heightli ");

}

}

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

Two. Structural body

1. Definition:

Access modifier struct struct name

{

Structural body

}

(1) Structs can have fields, methods, fields cannot be assigned initial values.

(2) can not new, but conditional, the struct has member variables and member methods, member variables are not assigned, call member methods, not new will error, so generally first new.

Three: Box packing and unpacking

The process of converting a value type to a reference type is called boxing, which is called unpacking

Practical development to avoid loading/unpacking as much as possible

Reason:

Loading/unpacking can reduce program performance

Four: Different types of parameter passing

Overview:

With value passing, changes to parameter values in a method cannot be persisted after the call.

Passed using ref mode, you can preserve changes to the parameter values.

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.