Search This Blog

2009-09-07

Difference between ref and out

The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed.you can not use the out paramter to pass a value to the method. With ref you can.Example:

// NOTE: firstName and lastName have not been assigned yet. Their values may not be used.SplitName(fullName, outfirstName, outlastName);// NOTE: firstName and lastName have been assigned, because the out parameter passing mode guarantees it.

System.Console.WriteLine("First Name '{0}'. Last Name '{1}'", firstName, lastName);}}