This post originated from an RSS feed registered with .NET Buzz
by Eric Gunnerson.
Original Post: Why does C# require 'ref' and 'out' at both definition and use?
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
class Test { public void Process(ref int t) { ... } }
when you call it you need to specify 'ref':
Test t = new Test(); int val = 5;
t.Process(ref val);
We require you to include 'ref' or 'out' to improve the readability of your code. Anybody who looks at the call above can tell that the value of val might be changed during the call. Otherwise, you'd have to find the definition, which would be much more difficult.