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"