How to return by reference using function.Please provide me with complete funcion definition which returns by reference in c#.Is there any other way to pass reference? Please help.
-
Well, in 2018, that is possible with `C# 7` :) – Arghya C Jan 17 '18 at 20:08
4 Answers
This is a method that returns an object reference by value:
string Foo()
{
return "Hello";
}
string f = Foo();
// f == "Hello"
You can't return anything by reference, but you can pass a variable by reference as an argument:
void Bar(ref string s)
{
s = "Hello";
}
string f = null;
Bar(ref f);
// f == "Hello"

- 213,145
- 36
- 401
- 431
-
If Foo is a reference type is "ref" still needed? I thought it was only for value types. – JustBoo Aug 21 '10 at 12:28
-
2
-
1@JustBoo: passing a reference type allows you to modify the value. Passing a reference type with the `ref` keyword allows you to re-seat the reference. – Merlyn Morgan-Graham Aug 21 '10 at 13:01
-
1@Merlyn: note that @dtb refers to the variable at the call-site, not the method parameter. – Fredrik Mörk Aug 21 '10 at 13:10
-
@Fredrik: I think at this point everyone is talking about the same thing :) – Merlyn Morgan-Graham Aug 21 '10 at 22:57
With the risk of sounding mean, you should read the C# reference :)
C# divides things into reference types and value types. Reference types are as you can imagine, being passed by reference. This means a reference to the object is passed.
Here is a contrived example of return by reference:
class MyClass // <- Reference type.
{
private MyClass _child = new MyClass();
public MyClass GetChild()
{
return _child;
}
}
Value types are passed by value; although I imagine under the hood something else could be going on. This is not important to you though, only the behaviour is important.
Example of value types: int
, char
, Color
...
You create a reference type through class
, and a value type through struct
.

- 21,515
- 5
- 53
- 66
-
4Keep in mind that "passed by reference" is an ambiguous concept many people will argue about. The precise version would be "by default the object reference is passed as value" and "ref keyword passes the reference to the object reference", or something similar. – viraptor Aug 21 '10 at 15:43
-
viraptor: I agree; just kept it simple since I don't actually know what kind of reference he was referring to. – Skurmedel Aug 21 '10 at 16:57
-
1It's not even ambiguous, it's plain wrong. C# by default always passes by value. Even reference types. To illustrate this, e.g. pass a `List
() { "Cat", "Dog" }` from scopeA to scopeB, set it in scopeB to `new List – Mike Fuchs Oct 04 '12 at 12:13()`, and check in scopeA, your pets will still be there. -
5For someone with C++ background, the question is valid and the C#'s way of handling this is not very obvious. If there is a function `List
FindIntegers(object anObject);` and another function `List – ThunderGr Nov 30 '12 at 09:02FindIntegers(ref object anObject)` the following are not very clear: 1) the returned list, should it happen to be a class or global, will be a reference of that class or global, or a copy of it?(e.g. if within FindIntegers there is `return MyGlobalIntegerList`) and 2) Both functions will have the parameter as a reference or the second one will get a reference of the reference? -
@adabyron Although your example appears to illustrate your assertion, it's not quite right. The reason it happens the way you say it is not because the List is passed by value, it's because without the "ref" keyword in the params list, the reference can be reassigned in the FindIntegers method, and your example does that. See: http://www.yoda.arachsys.com/csharp/parameters.html – Jace Nov 27 '16 at 05:35
-
I don't want to sound mean but the answer is incorrect. Even reference types are passed by value i.e. a copy of pointer is created. reassigning that pointer to null or a new object will not change the original object. In C#, passing by reference is achieved using ref keyword. I don't feel the need to answer this question / show examples here as the linked answer clarifies this pretty well. I would just encourage marking this as not answer because it could potentially confuse a learner. – stripathi Jan 03 '17 at 22:20
-
@stripathi I can't unmark my own answer and I'm still not sure exactly what mode of operation the OP desired. – Skurmedel Jan 05 '17 at 17:28
-
@Skurmedel - Maybe OP will unmark when he sees it. On an afterthought, your answer however technically incorrect may have answered what OP was trying to ask. As mentioned in the linked answer, C# does not have the functionality to return by reference anyway. But I believe OP was just trying to modify the return of a function which will work as long as you are not reassigning the return value. – stripathi Jan 06 '17 at 17:33
-
I normally consider reference types to behave like pointer types in C, with `.` obviously being equivalent to `->` (and automatic dereference on operators). – Ferazhu Nov 24 '22 at 23:41
Returning a reference is a C++ term. An example that demonstrates the syntax:
class Array { // This is C++ code
public:
int size() const;
float& operator[] (int index);
...
};
int main()
{
Array a;
for (int i = 0; i < a.size(); ++i)
a[i] = 7; // This line invokes Array::operator[](int)
...
}
C# doesn't have this capability, it is quite incompatible with the notion of a garbage collector. C++ implements this under the hood by actually returning a pointer. This falls flat when the garbage collector moves the underlying internal array, the pointer becomes invalid.
It is not much of a problem because variables of an object are already true references in managed code, much like the C++ notion of a reference. But not value types, they are copied. You'd use an indexer in this specific example:
class Array<T> { // This is C# code
private T[] storage;
public T this[int index] {
get { return storage[index]; }
set { storage[index] = value; }
}
// etc...
}

- 922,412
- 146
- 1,693
- 2,536
If you return a reference type from a function you have the object by reference. You may want to read up on refence and value types and passing parameter (by ref and out). See CLR via C#

- 1,644
- 1
- 15
- 23