Questions tagged [ref]

The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method. The value of a reference parameter is always the same as the value of the underlying argument variable.

1394 questions
1010
votes
28 answers

What's the difference between the 'ref' and 'out' keywords?

I'm creating a function where I need to pass an object so that it can be modified by the function. What is the difference between: public void myFunction(ref MyClass someClass) and public void myFunction(out MyClass someClass) Which should I use…
TK.
  • 46,577
  • 46
  • 119
  • 147
397
votes
15 answers

Assigning out/ref parameters in Moq

Is it possible to assign an out/ref parameter using Moq (3.0+)? I've looked at using Callback(), but Action<> does not support ref parameters because it's based on generics. I'd also preferably like to put a constraint (It.Is) on the input of the…
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
341
votes
11 answers

Why use the 'ref' keyword when passing an object?

If I am passing an object to a method, why should I use the ref keyword? Isn't this the default behaviour anyway? For example: class Program { static void Main(string[] args) { TestRef t = new TestRef(); t.Something =…
Ryan
  • 4,671
  • 4
  • 23
  • 21
115
votes
10 answers

When to use ref and when it is not necessary in C#

I have a object that is my in memory state of the program and also have some other worker functions that I pass the object to to modify the state. I have been passing it by ref to the worker functions. However I came across the following…
Rex Logan
  • 26,248
  • 10
  • 35
  • 48
107
votes
4 answers

C++ Difference between std::ref(T) and T&?

I have some questions regarding this program: #include #include #include using namespace std; template void foo ( T x ) { auto r=ref(x); cout<
DevInd
  • 1,645
  • 2
  • 11
  • 17
83
votes
4 answers

What would be a "Hello, World!" example for "std::ref"?

Can somebody give a simple example which demonstrates the functionality of std::ref? I mean an example in which some other constructs (like tuples, or data type templates) are used only if it is impossible to explain std::ref without them. I found…
Roman
  • 124,451
  • 167
  • 349
  • 456
69
votes
3 answers

Is this std::ref behaviour logical?

Consider this code: #include #include int xx = 7; template void f1(T arg) { arg += xx; } template void f2(T arg) { arg = xx; } int main() { int j; j=100; f1(std::ref(j)); …
oz1cz
  • 5,504
  • 6
  • 38
  • 58
60
votes
3 answers

Interesting interview exercise result: return, post increment and ref behavior

Here's a simple console application code, which returns a result I do not understand completely. Try to think whether it outputs 0, 1 or 2 in console: using System; namespace ConsoleApplication { class Program { static void Main() …
Aremyst
  • 1,480
  • 2
  • 19
  • 33
60
votes
1 answer

How to properly reference local resources in HTML?

As it turns out, referencing local resources can be a rub point for some. I'm looking for a canonical answer to local resource referencing, and what they mean. Take these examples, what is the difference between these reference paths?
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
52
votes
8 answers

current is always null when using React.createRef

I was trying to do this. I must be missing something, but I don't understand why current is always null in this example. class App extends React.PureComponent { constructor(props) { super(props); this.test = React.createRef(); } …
Sharcoux
  • 5,546
  • 7
  • 45
  • 78
51
votes
9 answers

C#: How to pass null to a function expecting a ref?

I've got the following function: public static extern uint FILES_GetMemoryMapping( [MarshalAs(UnmanagedType.LPStr)] string pPathFile, out ushort Size, [MarshalAs(UnmanagedType.LPStr)] string MapName, out ushort PacketSize, ref…
Nick
  • 2,913
  • 12
  • 40
  • 52
50
votes
2 answers

How can I use forwardRef() in React?

I'm currently receiving the following error in my React app: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? How can I fix this using forwardRef()? My code is as…
BEDev
  • 699
  • 1
  • 5
  • 9
50
votes
3 answers

React - useRef with TypeScript and functional component

I'm trying to call the child component method from the parent component using useRef. In the future, the SayHi method will update the hook state in the child component. Unfortunately, I have bugs I can't deal with. Line:…
Adam Nowicki
  • 504
  • 1
  • 4
  • 9
49
votes
5 answers

Why can't iterator methods take either 'ref' or 'out' parameters?

I tried this earlier today: public interface IFoo { IEnumerable GetItems_A( ref int somethingElse ); IEnumerable GetItems_B( ref int somethingElse ); } public class Bar : IFoo { public IEnumerable GetItems_A( ref int…
Trap
  • 12,050
  • 15
  • 55
  • 67
47
votes
4 answers

React ref.current is null

I'm working on an agenda/calendar app with a variable time range. To display a line for the current time and show blocks for appointments that have been made, I need to calculate how many pixels correspond with one minute inside the given time…
Thore
  • 1,918
  • 2
  • 25
  • 50
1
2 3
92 93