Skip to content

Instantly share code, notes, and snippets.

@jorgecolonconsulting
Last active January 22, 2020 11:18
Show Gist options
  • Save jorgecolonconsulting/6d98a57b68d849ee7091 to your computer and use it in GitHub Desktop.
Save jorgecolonconsulting/6d98a57b68d849ee7091 to your computer and use it in GitHub Desktop.
C# ref vs out in function/method parameter
string message = "hello";
public void SayMessage(ref string message) {
Console.WriteLine(message);
message = "hello in here";
}
SayMessage(ref message); // "hello"
Console.WriteLine(message); // "hello in here"
// run this code: http://csharppad.com/gist/ca901eb8e7d995a1f29c
string message = "hello";
public void SayMessage(out string message) {
message = "hello in here";
Console.WriteLine(message);
}
SayMessage(out message); // "hello in here"
Console.WriteLine(message); // "hello in here"
// run this code: http://csharppad.com/gist/8e4c1dd9b153b3ef7d8f
string message = "hello";
public void SayMessage(out string message) {
Console.WriteLine(message); // (4,23): error CS0269: Use of unassigned out parameter 'message'
message = "hello in here";
}
SayMessage(out message);
Console.WriteLine(message);
// run this code: http://csharppad.com/gist/8e4c1dd9b153b3ef7d8f
string message = "hello";
public void SayMessage(string message) {
message = "hello in here";
Console.WriteLine(message);
}
SayMessage(message); // "message"
Console.WriteLine(message); // "message"
// run this code: http://csharppad.com/gist/8e4c1dd9b153b3ef7d8f
@jorgecolonconsulting
Copy link
Author

@Rabbles glad to hear!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment