C # Fundamentals: Value types, reference types, and ref keywords

Source: Internet
Author: User

View Plaincopy to Clipboardprint?
    1. int a = ten, B = 20;
    2. void swap (int x, int y)
    3. {
    4. int temp = x;
    5. x = y;
    6. y = temp;
    7. }

If you simply call this swap, such as swap (A, b), then you simply cannot exchange the values of the two variables, because both x and Y are formal parameters, and X and Y are freed when the swap returns. But if this is the definition of swap:

View Plaincopy to Clipboardprint?
    1. void Swap (int& x, int& y)
    2. {
    3. int temp = x;
    4. x = y;
    5. y = temp;
    6. }

The same as x and A,y and B point to the same memory address, then the operation of X is equivalent to the operation of A. So in C #, this effect is obvious for value types.

View Plaincopy to Clipboardprint?
  1. Class Program
  2. {
  3. static void Test (ref int b)
  4. {
  5. b = 2;
  6. }
  7. static void Main (string[] args)
  8. {
  9. int b = 1;
  10. Test (ref B);
  11. Console.WriteLine (b);
  12. }
  13. }

The output at this point is 2, that is, b in the test method points to the same memory address as B in main, then the operation on TEST.B is the main.b operation. If you change the program to:

View Plaincopy to Clipboardprint?
  1. Class Program
  2. {
  3. static void Test (int b)
  4. {
  5. b = 2;
  6. }
  7. static void Main (string[] args)
  8. {
  9. int b = 1;
  10. Test (b);
  11. Console.WriteLine (b);
  12. }
  13. }

Then the output is still 1, because test.b is not a main.b reference, that is, a single parameter. Now look at the effect of the reference type:

View Plaincopy to Clipboardprint?
  1. Class TestClass
  2. {
  3. public int B;
  4. }
  5. Class Program
  6. {
  7. static void Test (TestClass b)
  8. {
  9. B.B = 2;
  10. }
  11. static void Main (string[] args)
  12. {
  13. TestClass B = new TestClass ();
  14. b.b = 1;
  15. Test (b);
  16. Console.WriteLine (B.B);
  17. }
  18. }

The above code, the output is 2, because B is a reference type, when you only need to modify the members of B, add the same as the REF keyword. The reference type itself does not contain data, and it only maintains a reference to the data.

Therefore, using the ref parameter, the effect on a value type object is obvious, and for reference types, you do not need to use the REF keyword if you want to modify the data inside the reference type, otherwise, when the called function internally needs to change the reference itself, such as to reposition the reference to the object inside the function, You need to use the REF keyword.

C # Fundamentals: Value types, reference types, and ref keywords

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.