The out parameters are very similar to the ref parameters. In fact, the difference is that the out parameters don't need to be initialized before they are passed to some method. For example: if there's a variable called a, that has not been initialized ( no value assigned yet ), and need to be passed to be modified, the out parameter would be the answer. If the same variable is passed as ref, then you will receive a compilation error.
The next example would explaint it all: a method that changes the value of a variable not initialized.
// // out-parameters.cs // We change the value of the variable 'a' // using System; class Tester { public void ChangeVal ( out int a ) { a = 7; } public static void Main () { Tester t = new Tester(); int a; // a has no received value assignment t.ChangeVal(out a); Console.WriteLine(" The value of 'a' is {0}", a); } }
The variables/fields passed with the out keyword don't need to be initialized. But if we try to work with its value inside the method, it won't compile. There's a big difference between assigning a value, and trying to work with its unassigned value. Remember: a variable without value assigned can only receive a value, and when the variable has finally value, then it value can be used.
Save, compile and run it. You should see:
The value of 'a' is 7