13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Implicitly typed arrays2238.4 Implicitly typed arraysIn <strong>C#</strong> 1 and 2, <strong>in</strong>itializ<strong>in</strong>g an array as part of a variable declaration and <strong>in</strong>itializationstatement was quite neat—but if you wanted to do it anywhere else, you had to specifythe exact array type <strong>in</strong>volved. So for example, this compiles without any problem:str<strong>in</strong>g[] names = {"Holly", "Jon", "Tom", "Rob<strong>in</strong>", "William"};This doesn’t work for parameters, though: suppose we want to make a call toMyMethod, declared as void MyMethod(str<strong>in</strong>g[] names). This code won’t work:MyMethod({"Holly", "Jon", "Tom", "Rob<strong>in</strong>", "William"});Instead, you have to tell the compiler what type of array you want to <strong>in</strong>itialize:MyMethod(new str<strong>in</strong>g[] {"Holly", "Jon", "Tom", "Rob<strong>in</strong>", "William"});<strong>C#</strong> 3 allows someth<strong>in</strong>g <strong>in</strong> between:MyMethod(new[] {"Holly", "Jon", "Tom", "Rob<strong>in</strong>", "William"});Clearly the compiler needs to work out what type of array to use. It starts by form<strong>in</strong>g aset conta<strong>in</strong><strong>in</strong>g all the compile-time types of the expressions <strong>in</strong>side the braces. Ifthere’s exactly one type <strong>in</strong> that set that all the others can be implicitly converted to,that’s the type of the array. Otherwise, (or if all the values are typeless expressions,such as constant null values or anonymous methods, with no casts) the code won’tcompile. Note that only the types of the expressions are considered as candidates forthe overall array type. This means that occasionally you might have to explicitly cast avalue to a less specific type. For <strong>in</strong>stance, this won’t compile:new[] { new MemoryStream(), new Str<strong>in</strong>gWriter() }There’s no conversion from MemoryStream to Str<strong>in</strong>gWriter, or vice versa. Both areimplicitly convertible to object and IDisposable, but the compiler only considers typesthat are <strong>in</strong> the orig<strong>in</strong>al set produced by the expressions themselves. If we change oneof the expressions <strong>in</strong> this situation so that its type is either object or IDisposable, thecode compiles:new[] { (IDisposable) new MemoryStream(), new Str<strong>in</strong>gWriter() }The type of this last expression is implicitly IDisposable[]. Of course, at that po<strong>in</strong>tyou might as well explicitly state the type of the array just as you would <strong>in</strong> <strong>C#</strong> 1 and 2,to make it clearer what you’re try<strong>in</strong>g to achieve.Compared with the earlier features, implicitly typed arrays are a bit of an anticlimax.I f<strong>in</strong>d it hard to get particularly excited about them, even though they do makelife that bit simpler <strong>in</strong> cases where an array is passed as a parameter. You could wellargue that this feature doesn’t prove itself <strong>in</strong> the “usefulness versus complexity” balanceused by the language designers to decide what should be part of the language.The designers haven’t gone mad, however—there’s one important situation <strong>in</strong>which this implicit typ<strong>in</strong>g is absolutely crucial. That’s when you don’t know (and<strong>in</strong>deed can’t know) the name of the type of the elements of the array. How can youpossibly get <strong>in</strong>to this peculiar state? Read on…Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!