Prev | Current Page 170 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

)
NOTE The TryXXX pattern??”There are a few patterns in .NET that are easily identifiable
by the names of the methods involved??”BeginXXX and EndXXX
suggest an asynchronous operation, for example. The TryXXX pattern is
one that has had its use expanded between .NET 1.1 and 2.0. It??™s
designed for situations that might normally be considered to be errors
(in that the method can??™t perform its primary duty) but where failure
could well occur without this indicating a serious issue, and shouldn??™t be
deemed exceptional. For instance, users can often fail to type in numbers
correctly, so being able to try to parse some text without having to catch
an exception and swallow it is very useful. Not only does it improve performance
in the failure case, but more importantly, it saves exceptions
for genuine error cases where something is wrong in the system (however
widely you wish to interpret that). It??™s a useful pattern to have up your
sleeve as a library designer, when applied appropriately.
C# 2 provides the default value expression to cater for just this need. The specification
doesn??™t refer to it as an operator, but you can think of it as being similar to the typeof
operator, just returning a different value. Listing 3.4 shows this in a generic method,
and also gives an example of type inference and a derivation type constraint in action.


Pages:
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182