Information Technology Reference
In-Depth Information
This code produces the following output:
Value: 12
Figure 15-10. The value of a reference parameter can change between calls.
Anonymous Methods
So far, you've seen that you can use either static methods or instance methods to instantiate a
delegate. In either case, the method itself can be called explicitly from other parts of the code,
and, of course, must be a member of some class or struct.
What if, however, the method is used only one time—to instantiate the delegate? In that
case, other than the syntactic requirement for creating the delegate, there is no real need for a
separate, named method. C# 2.0 introduces the anonymous method, which allows you to dis-
pense with the separate, named method.
￿An anonymous method is a method that is declared in-line, at the point of instantiating
a delegate.
For example, Figure 15-11 shows two versions of the same class. The version on the left
declares and uses a method named Add20 . The version on the right uses an anonymous method
instead. The nonshaded code of both versions is identical.
Search WWH ::




Custom Search