Summary of C # ref and out pass parameters

Source: Internet
Author: User

If there is similar, it is honored, if reproduced, please specify

C # in the ref and out pass parameter summary, two can be used to pass parameters, ref must be initialized before use, out is not required, as long as the return before the assignment, text nonsense to this, the following direct example

ref example

Class A

{

private string name = String. Empty;

private int count = 0;

...

GetName (ref name,ref count);

Console.Write ("Name:" + name + ", Quantity:" + count);

}

Class B

{

...

The parameter name is 1 pointers to the stack that have a value type of string
      The parameter count is a pointer to 1 that points to the value type int in the stack

public bool GetName (ref string Name,ref int count)

{

...

name = "Ching";

Count = 10;

return true;

}

}

Output Result: Name: Ching, Quantity: 10

out example

Class A

{

private string name;

private int count;

...

GetName (ref name,ref count);

Console.Write ("Name:" + name + ", Quantity:" + count);

}

Class B

{

...

public bool GetName (ref string Name,ref int count)

{

...

name = "Ching";

Count = 10;

return true;

}

}

The same output results: Name: Ching, Quantity: 10

To sum up, ref and out in C # have the same effect, just note that ref needs to initialize the value first, out is not required, but to be assigned a value before returning, the above is a simple elaboration, detailed introduction or confusion in the use of the official version of the MSDN manual can be, welcome everyone treatise

Summary of C # ref and out pass parameters

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.