Reputation: 3289
How to pass a parameter as a Reference in C# and when calling the function how should i represent it while defining that particular function
Upvotes: 3
Views: 13450
Reputation: 1499770
As others have said, you should use the ref
modifier at both the call site and the method declaration to indicate that you want to use by-reference semantics. However, you should understand how by-value and by-reference semantics interact with the "value type" vs "reference type" model of .NET.
I have two articles on this:
Upvotes: 11
Reputation: 7673
you can also reference types without need to user ref
keyword
For example you can bass an instance of Class to a method as a parameter without ref
keyword
Upvotes: 0
Reputation: 43207
Just add ref keyword as prefix to all arguments in the function definition. Put ref keyword prefix in arguments passed while calling the function.
Upvotes: 0
Reputation: 25513
Here's an example of passing an int by reference:
void DoubleInt(ref int x)
{
x += x;
}
and you would call it like this:
DoubleInt(ref myInt);
Here's an msdn article about passing parameters.
Upvotes: 13
Reputation: 27226
I suggest you take a look at the MSDN documentation about this. It's very clear.
Upvotes: 2
Reputation: 73301
You can use the ref keyword to pass an object by refence.
public void YourFunction(ref SomeObject yourReferenceParameter)
{
// Do something
}
When you call it you are also required to give the ref keyword.
SomeObject myLocal = new SomeObject();
YourFunction(ref myLocal);
Upvotes: 5