Questions tagged [ref-parameters]
16 questions
130
votes
10 answers
Why doesn't 'ref' and 'out' support polymorphism?
Take the following:
class A {}
class B : A {}
class C
{
C()
{
var b = new B();
Foo(b);
Foo2(ref b); // <= compile-time error:
// "The 'ref' argument doesn't match the parameter type"
}
…

Andreas Grech
- 105,982
- 98
- 297
- 360
18
votes
6 answers
In what situations are 'out' parameters useful (where 'ref' couldn't be used instead)?
As far as I can tell, the only use for out parameters is that a caller can obtain multiple return values from a single method invocation. But we can also obtain multiple result values using ref parameters instead!
So are there other situations where…

flockofcode
- 1,799
- 2
- 12
- 21
15
votes
4 answers
Why is an out parameter not allowed within an anonymous method?
This is not a dupe of Calling a method with ref or out parameters from an anonymous method
I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters,…

Chris Ballance
- 33,810
- 26
- 104
- 151
14
votes
2 answers
When is the value of a C# 'out' or 'ref' parameter actually returned to the caller?
When I make an assignment to an out or ref parameter, is the value immediately assigned to the reference provided by the caller, or are the out and ref parameter values assigned to the references when the method returns? If the method throws an…

Zach Johnson
- 23,678
- 6
- 69
- 86
11
votes
4 answers
ref Parameter and Assignment in same line
I ran into a nasty bug recently and the simplified code looks like below:
int x = 0;
x += Increment(ref x);
...
private int Increment(ref int parameter) {
parameter += 1;
return 1;
}
The value of x after the Increment call is 1! This was…

Michael
- 3,222
- 2
- 21
- 22
9
votes
4 answers
Passing an explicit cast as a ref parameter (C#)
I have a class that is mostly a wrapper for a big array and some associated housekeeping. I have a function that takes a ref parameter. When I pass an instance of the class into the function, I want the array to get sent.
I considered explicit…

ajs410
- 2,384
- 2
- 19
- 14
7
votes
3 answers
How to convert out/ref extern parameters to F#
I've got a C# extern declaration that goes like this:
[DllImport("something.dll")]
public static extern ReturnCode GetParent(IntPtr inRef, out IntPtr outParentRef);
How to translate that to F#?

Dax Fohl
- 10,654
- 6
- 46
- 90
7
votes
1 answer
Why ref parameters can not be ignored like out parameters?
Is there a specific reason why C# 7 bring inlining out parameters but not ref?
The following is valid on C# 7:
int.TryParse("123", out _);
But this is invalid:
public void Foo(ref int x) { }
Foo(ref _); // error
I don't see a reason why the same…

Selman Genç
- 100,147
- 13
- 119
- 184
3
votes
2 answers
How to swap property values in ViewModel? Can't pass by ref
I'd like to compare and swap the values of two ViewModel public properties, ideally using a method like this:
void SwapIfGreater(ref T lhs, ref T rhs) where T : System.IComparable
{
T temp;
if (lhs.CompareTo(rhs) > 0)
{
…

RichardD
- 315
- 1
- 4
- 11
3
votes
3 answers
Good naming convention for functions and methods which may modify (write back to) a parameter
I need to find a good and understandable naming-scheme for routines which deal with "value arrays" (I've written something similar to C++'s valarray in Java, with some optimizations for primitive types in mind).
I know that the main categorization…

java.is.for.desktop
- 10,748
- 12
- 69
- 103
2
votes
4 answers
What happens on the stack with ref parameters in c#?
Im reading some C# documentation about WCF and IDispatchMessageInspector and the interface defines a 'Message' object that is passed by reference so that it can be manipulated.
What actually happens on the stack when you pass something by ref as…

Exitos
- 29,230
- 38
- 123
- 178
2
votes
4 answers
How to convert recursive procedure with side effects on ref param to recursive function returning a list?
It seems every time I go to write a recursive function I end up making it return void and using a ref parameter.
I'd much rather be able to write a function that just returns a result list.
Apologies if the answer is very simple - for some reason it…

Aaron Anodide
- 16,906
- 15
- 62
- 121
2
votes
2 answers
Why can't I pass indexer as a ref parameter?
I understand the rule. But who knows why this is?
If my code is:
List x = new List;
x.Add(new T());
x.Add(new T());
x.Add(new T());
int y = 2;
//And I call a method
M1(ref x[y]);
ref x[y] is just a pointer to my instance of T of interest…

Steve
- 905
- 1
- 8
- 32
0
votes
5 answers
How to downcast a ref variable within the method
I need to downcast a long to an int in a method where the long is passed as a ref variable:
public void Foo(ref long l)
{
// need to consume l as an int
}
How can I easily do this?

jyotishka bora
- 3,963
- 2
- 23
- 19
0
votes
2 answers
Passing and updating reference type parameters in function
Consider I have object A which is holding reference to B object type, but this time initilized to null.
A->B( == null)
I want to replace the null with object of type B which hold reference to type C.
(B->C).
So I will get A->B->C .
Why isn't…

JavaSa
- 5,813
- 16
- 71
- 121