Ref out keywords

ref parameters
Passing variables by value is the default. We can, however, force value parameters to be passed by reference.
To do so, we use the ref keyword. If a parameter is passed to a method, and if the input argument
for that method is prefixed with the ref keyword, then any changes that the method makes to the variable
will affect the value of the original object:
static void SomeFunction(int[] ints, ref int i)
{
ints[0] = 100;
i = 100; // the change to i will persist after SomeFunction() exits
}
We will also need to add the ref keyword when we invoke the method:
SomeFunction(ints, ref i);
90
Chapter 3
Adding the ref keyword in C# serves the same purpose as using the & syntax in C++ to specify passing
by reference. However, C# makes the behavior more explicit (thus hopefully preventing bugs) by requiring
the use of the ref keyword when invoking the method.
Finally, it is also important to understand that C# continues to apply initialization requirements to
parameters passed to methods. Any variable must be initialized before it is passed into a method,
whether it is passed in by value or reference.
out parameters
In C-style languages, it is common for functions to be able to output more than one value from a single
routine. This is accomplished using output parameters, by assigning the output values to variables that
have been passed to the method by reference. Often, the starting values of the variables that are passed
by reference are unimportant. Those values will be overwritten by the function, which may never even
look at any previous value.
It would be convenient if we could use the same convention in C#. However, C# requires that variables
be initialized with a starting value before they are referenced. Although we could initialize our input
variables with meaningless values before passing them into a function that will fill them with real,
meaningful ones, this practice seems at best needless and at worst confusing. However, there is a way
to short-circuit the C# compiler’s insistence on initial values for input arguments.
This is achieved with the out keyword. When a method’s input argument is prefixed with out, that
method can be passed a variable that has not been initialized. The variable is passed by reference, so any
changes that the method makes to the variable will persist when control returns from the called method.
Again, we also need to use the out keyword when we call the method, as well as when we define it:
static void SomeFunction(out int i)
{
i = 100;
}
public static int Main()
{
int i; // note how i is declared but not initialized
SomeFunction(out i);
Console.WriteLine(i);
return 0;
}
The out keyword is an example of something new in C# that has no analogy in either Visual Basic or
C++, and which has been introduced to make C# more secure against bugs. If an out parameter isn’t
assigned a value within the body of the function, the method won’t compile.
Method overloading
C# supports method overloading—several versions of the method that have different signatures (that is,
the name, number of parameters, and parameter types). However, C# does not support default parameters
in the way that, say, C++ or Visual Basic do. In order to overload methods, you simply declare the
methods with the same name but different numbers or types of parameters:
91
Objects and Types
class ResultDisplayer
{
void DisplayResult(string result)
{
// implementation
}
void DisplayResult(int result)
{
// implementation
}
}
Because C# does not support optional parameters, you will need to use method overloading to achieve
the same effect:
class MyClass
{
int DoSomething(int x) // want 2nd parameter with default value 10
{
DoSomething(x, 10);
}
int DoSomething(int x, int y)
{
// implementation
}
}
As in any language, method overloading carries with it the potential for subtle runtime bugs if the
wrong overload is called. In Chapter 4 we discuss how to code defensively against these problems. For
now, we’ll point out that C# does place some minimum differences on the parameters of overloaded
methods.
❑ It is not sufficient for two methods to differ only in their return type.
❑ It is not sufficient for two methods to differ only by virtue of a parameter having been declared
as ref or out.
From "Pro C# 3rd"
posted @ 2006-04-16 21:47  徐灿钊Asp.net专栏  阅读(488)  评论(1编辑  收藏  举报