Understanding C # Value types and reference types

Source: Internet
Author: User

value types and reference types
Most basic data types in C # are value types, except that the string type is a reference type;
A class is a reference type, and a struct is a value type;
null values and nullable types
A null value is useful when initializing a reference type, but null itself is a reference and cannot be assigned to a value type, in C # The following statement is illegal: int i=null;//not legal
However, with a modifier defined by C #, you can declare a variable as aNullable (Nullable)The value type. A nullable value type is similar in behavior to a normal value type, but a null value can be assigned to it. We use a question mark(?)To make a value type nullable, as follows:
Int? i=null;//Legal
To determine whether a nullable variable is empty, you can determine the same way as a reference type:
if (i==null)
An expression with the appropriate value type can be assigned directly to a nullable variable. Otherwise, the null variable can not assign values to ordinary value type variables.
Understanding nullable types of properties
Nullable types reveal two properties: The HasValue property indicates whether a nullable type contains a true value or null, and if it contains a true value, it can be obtained using the Value property.

Int? I=null;                          
 ....
if (!i.hasvalue)             
      i=99;                                
Else                                  
     Console.WriteLine (i.value);                              

If I do not contain a real value, that is NULL, assign 99 to it, otherwise, print its value.
Note: The Value property of a nullable type is read-only and cannot be modified, and the values of nullable variables are modified, using normal assignment statements.
The memory organization of the computer--heap and stack
when a method is invoked, the memory required by its arguments and local variables is always fetched from the stack;
Using the new keyword to create an object, the memory required to construct the object is always fetched from the heap;
Stacks are like a series of increasingly high boxes, accessed sequentially, while the heap is like a pile of boxes scattered around the room, and when the object is created, the "runtime" finds an empty box and assigns it to the object;
Reference types are placed on the heap, and value types are placed on the stack.

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.