In C#, ref, out, and in are keywords used to pass arguments to methods by reference rather than by value.
Passing variables by reference
Consider this. We want to modify a local variable in the SetFullName() method:
private static void SetFullName(string nameToSet)
{
nameToSet = "Mirza Leka";
}
But the SetFullName() method does not return the modified name. Thus, when we invoke the method from the outside, the calling method still has the old name.
internal class Program
{
static void Main(string[] args)
{
var name = "Mirza";
SetFullName(name);
Console.WriteLine(name); // "Mirza"
Console.ReadLine();
}
private static void SetFullName(string nameToSet)
{
nameToSet = "Mirza Leka";
}
}
How do we get around that? How do we get back the modified name without explicitly returning the name as follows:
// common practice
private static string GetFullName(string nameToSet)
{
nameToSet = "Mirza Leka";
return nameToSet;
}
Ref
The ref keyword allows a variable to be passed by reference to other methods.
private static void SetFullName(ref string nameToSet)
{
nameToSet = "Mirza Leka";
}
With ref in place, any changes made to the name will be visible in the calling method:
internal class Program
{
static void Main(string[] args)
{
var name = "Mirza";
SetFullName(ref name);
Console.WriteLine(name); // "Mirza Leka"
Console.ReadLine();
}
private static void SetFullName(string nameToSet)
{
nameToSet = "Mirza Leka";
}
}
Pros & Cons:
- ✅ Let's use a method to modify the caller's variable directly, without needing to return it.
- ✅ Avoids copying large structs into the method.
- ❌ The variable must be initialized before it's passed in.
Quick note: We do not need to use the ref keyword when passing variables that are reference types.
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
var person = new Person { Id = 1, Name = name };
SetFullName(person);
Console.WriteLine(person.Name); // "Mirza Leka"
Console.ReadLine();
}
private static void SetFullName(Person p)
{
p.Name = "Mirza Leka";
}
}
Read-Only arguments
Say we want to create a logging method that can only read the name variable, but cannot modify it. That's where the in keyword comes in.
When you set the in next to the argument, it signals to the compiler that the parameter is read-only.
private static void LogFullName(in string nameToLog)
{
Console.WriteLine(nameToLog);
}
internal class Program
{
static void Main(string[] args)
{
var name = "Mirza";
LogFullName(name); // "Mirza"
Console.ReadLine();
}
private static void LogFullName(in string nameToLog)
{
Console.WriteLine(nameToLog);
}
}
Modifications aren't allowed.
private static void LogFullName(in string nameToLog)
{
//nameToLog = "Mirza Leka"; ❌
Console.WriteLine(nameToLog);
}
Pros & Cons:
- ✅ Avoids copying large structs into the method, the same way
refdoes, but without allowing the method to mutate them. - ✅ Makes intent explicit — readers know the argument is read-only inside the method.
- ❌ Only prevents reassigning the parameter itself. If it's a reference type, its members can still be mutated.
More than one return
Ever been in a situation where you wanted to add a response without changing the original return type?
Out
The out keyword lets us do just that - let the method return more than one response.
private static bool IsValidName(string name, out string errorMessage)
{
if (string.IsNullOrWhiteSpace(name))
{
errorMessage = "Name must be set!";
return false;
}
errorMessage = string.Empty;
return true;
}
The IsValid() method will still return a boolean, but we can also extract the error.
var isValid = IsValidName(name, out string errorMsg);
Now we can both validate the response and get the error message.
internal class Program
{
static void Main(string[] args)
{
var name = "Mirza";
var isValid = IsValidName(name, out string errorMsg);
if (!isValid)
{
Console.WriteLine($"Error: {errorMsg}");
}
else
{
Console.WriteLine(name);
}
Console.ReadLine();
}
}
Pros & Cons:
- ✅ A method can return multiple values without creating a custom return type (class or tuple).
- ✅ Commonly used in the validate-and-get-result pattern (like
TryParse,TryGetValue). - ❌ The parameter must be assigned inside the method before it returns.
Summary
Three keywords. Three questions to answer:
-
ref— does the method need to change the value, and should that change be visible to the caller? -
in— do you pass a value type and want to keep it read-only? -
out— does the method need to return more than one value?
Until next time 👋
Top comments (0)