Pass A Reference To A Function, Method, etc

Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment.

You just use the ‘ref’ or ‘out’ keyword:

‘ref’ tells the compiler that the object is initialized before entering the function

‘out’ tells the compiler that the object will be initialized inside the function

So ‘ref’ is two-ways and ‘out’ is out-only

	public void MyMethodName(ref int[] BookIds, ref string[] BookLanguages)
	{
	//or:
	public void MyMethodName(out int[] BookIds, out string[] BookLanguages)
	{

Calling the method

You need to use the ‘ref’ or ‘out’ keyword here too

	MyMethodName(ref TheBookIds, ref TheBookLanguages);
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *