HiveBrain v1.2.0
Get Started
← Back to all entries
gotchacsharpCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
keywordsandbetweenthedifferencerefoutwhat

Problem

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 and why?

Solution

ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.

So while ref is two-ways, out is out-only.

Context

Stack Overflow Q#388464, score: 1341

Revisions (0)

No revisions yet.