C # my views on the value type and reference type

Source: Internet
Author: User

The biggest difference between the value type and the reference type is that the storage space allocated by the value type is in the stack, and the Space analyzed by the reference type is in the heap memory.

So what types are value types and reference types?

Value Type: primitive type, enumeration type, and struct type.

Reference Type: Class, String, standard module, interface, array, and delegate.

So how can we differentiate between value types and reference types based on their table types?

Solution 1: Use the system type attribute to determine

Hide row number CopyCode ? This is Program Code.
    1. Public boolIsvaluetype <t> ()
    2. {
    3. Return typeof(T). isvaluetype;
    4. }

 

Solution 2: Use whether it can be empty or not to judge (trick)

Value reference cannot be empty by default. For example, int I = NULL; a prompt is displayed in the vs compiler.

The reference type can be null by default. For example, string STR = NULL is correct.

Value reference and type reference must be distinguished during programming. They are also often used to evaluate the basic skills of a programmer. They appear in interview questions or exams. How can they be investigated?

Based on: value-type variables directly contain their data, while reference-type variables store references to their data, which is called an object. For the reference type, two variables may reference the same object. Therefore, operations on one variable may affect the objects referenced by another variable.

You may encounter the following code to write the final output:

Hide row number Copy code ? This is a piece of program code.
  1. Public voidPrinttheresultstring ()
  2. {
  3. Console. Writeline ("String type value assignment");
  4. StringSTR ="Init";
  5. Console. Writeline ("Original :"+ Str );
  6. Process (STR );
  7. Console. Writeline ("Result :"+ Str );
  8. }
  9. Private voidProcess (StringStr)
  10. {
  11. STR ="Changed";
  12. }

 

Hide row number Copy code ? This is a piece of program code.
    1.   Public void  printtheresultint () 
    2. {
    3.   console . writeline ( "int type value assignment" ); 
    4.   int  I = 0; 
    5.   console . writeline ( "Original:"  + I); 
    6.  process (I); 
    7.   console . writeline ( "Result:"  + I); 
    8. }
    9.   private void  process ( int  I) 
    10. {
    11.  I = 2; 
    12. }

 

Hide row number Copy code ? This is a piece of program code.
  1. Public voidPrinttheresultdatetime ()
  2. {
  3. Console. Writeline ("Datetime type value assignment");
  4. DatetimeDt =Datetime. Parse ("2000-01-01");
  5. Console. Writeline ("Original :"+ Dt. tostring ("Yyyy-mm-dd"));
  6. Process (DT );
  7. Console. Writeline ("Result :"+ Dt. tostring ("Yyyy-mm-dd"));
  8. }
  9. Private voidProcess (DatetimeDT)
  10. {
  11. Dt =Datetime. Parse ("2001-01-01");
  12. }

 

Hide row number Copy code ? This is a piece of program code.
  1. Public voidPrinttheresultarraylist ()
  2. {
  3. Console. Writeline ("Arraylist type assignment");
  4. ArraylistArraylist =NewArraylist();
  5. Console. Writeline ("Original :"+ Arraylist. Count );
  6. Process (arraylist );
  7. Console. Writeline ("Result :"+ Arraylist. Count );
  8. }
  9. Private voidProcess (ArraylistArraylist)
  10. {
  11. Arraylist. Add ("1");
  12. }

 

In the preceding four paragraphs, what are the output results?

It can be seen that, except for the value assignment of the arraylist type, the values are changed, and the other values are kept in the initial state. Do string, Int, and datetime values all belong to the value type?

With solution 1, you can easily find that string is a reference type, and it is a spy in the value type. String is a reference type, but its usage is exactly the same as that of the value type. It is a pseudo-value type fabricated.

Int is a primitive type, datetime is a struct type, and they are indeed a real value type.

 

That's all. Let's talk about the empty type next time.

Related Article

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.