C # Object-Oriented Programming-ref parameter (V)

Source: Internet
Author: User

Ref Parameter

You can pass parameters not only by using the value as a method, but also by referencing as a method. If you pass parameters by referencing as a method, you need to use the ref keyword.

If you pass a parameter to a method with the ref keyword before the input parameter of this method, any change made to the parameter of this method will affect the value of the original object.

Note:

1: The reference parameter passed through ref must have been assigned a value before the function Member calls it. If the value is not explicitly assigned, an error is returned.

Instance

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace _ 5_ref
{
Class Program
{
Static void Main (string [] args)
{
String sex = "male ";
Person person = new Person ();
String name = person. GetName (ref sex );
Console. WriteLine ("{0} {1}", name, sex );
Console. ReadKey ();
}
}

Class Person
{
Public string GetName (ref string strSex)
{
If (strSex = "male ")
{
StrSex = "female ";
Return "Han Meimei ";
}
Else
{
StrSex = "male ";
Return "Li Lei ";
}
}
}
}

Running Effect

We can see that our sex value has changed to "female"

  

  

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.