Ref and out in C #

Source: Internet
Author: User

What I want to say is that the reason why -- ref and out confused me was that they did not seriously analyze the different actions it made for different types.

For value type.

The effect of using ref and out is almost the same as that of using pointer variables in C. It allows you to operate the original number directly, rather than the copy operation of the original number. For example:

Using
System;

Namespace consoleapplication4

{

///

/// Summary of class1.

///

Class class1

{

///

/// Main entry point of the application.

///

[Stathread]

Static void main (string [] ARGs)

{

Int A = 5;

Int B;

Squareref (Ref );

Squareout (Out B );

Console. writeline ("the in the main is:" + );

Console. writeline ("the B in the main is:" + B );

}

Static void squareref (ref int X)

{

X = x * X;

Console. writeline ("the X in the squareref is:" + x );

}

Static void squareout (Out int y)

{

Y = 10;

Y = y * Y;

Console. writeline ("the Y in the squareout is:" + y );

}

}

}

The result is -- 25 100 25 100.

In this way, it achieves the same effect as the pointer variable in C.

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.