13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

108 CHAPTER 3 Parameterized typ<strong>in</strong>g with generics3.6.3 Lack of generic properties, <strong>in</strong>dexers, and other member typesWe’ve seen generic types (classes, structs, delegates, and <strong>in</strong>terfaces) and we’ve seengeneric methods. There are plenty of other members that could be parameterized.However, there are no generic properties, <strong>in</strong>dexers, operators, constructors, f<strong>in</strong>alizers,or events. First let’s be clear about what we mean here: clearly an <strong>in</strong>dexer can havea return type that is a type parameter—List is an obvious example. KeyValue-Pair provides similar examples for properties. What you can’t have isan <strong>in</strong>dexer or property (or any of the other members <strong>in</strong> that list) with extra typeparameters. Leav<strong>in</strong>g the possible syntax of declaration aside for the m<strong>in</strong>ute, let’s lookat how these members might have to be called:SomeClass <strong>in</strong>stance = new SomeClass("x");<strong>in</strong>t x = <strong>in</strong>stance.SomeProperty;byte y = <strong>in</strong>stance.SomeIndexer["key"];<strong>in</strong>stance.Click += ByteHandler;<strong>in</strong>stance = <strong>in</strong>stance + <strong>in</strong>stance;I hope you’ll agree that all of those look somewhat silly. F<strong>in</strong>alizers can’t even be calledexplicitly from <strong>C#</strong> code, which is why there isn’t a l<strong>in</strong>e for them. The fact that we can’tdo any of these isn’t go<strong>in</strong>g to cause significant problems anywhere, as far as I cansee—it’s just worth be<strong>in</strong>g aware of it as an academic limitation.The one exception to this is possibly the constructor. However, a static genericmethod <strong>in</strong> the class is a good workaround for this, and the syntax with two lists of typearguments is horrific.These are by no means the only limitations of <strong>C#</strong> generics, but I believe they’re theones that you’re most likely to run up aga<strong>in</strong>st, either <strong>in</strong> your daily work, <strong>in</strong> communityconversations, or when idly consider<strong>in</strong>g the feature as a whole. In our next two sectionswe’ll see how some aspects of these aren’t issues <strong>in</strong> the two languages whose featuresare most commonly compared with <strong>C#</strong>’s generics: C++ (with templates) and Java(with generics as of Java 5). We’ll tackle C++ first.3.6.4 Comparison with C++ templatesC++ templates are a bit like macros taken to an extreme level. They’re <strong>in</strong>credibly powerful,but have costs associated with them both <strong>in</strong> terms of code bloat and ease ofunderstand<strong>in</strong>g.When a template is used <strong>in</strong> C++, the code is compiled for that particular set of templatearguments, as if the template arguments were <strong>in</strong> the source code. This means thatthere’s not as much need for constra<strong>in</strong>ts, as the compiler will check whether you’reallowed to do everyth<strong>in</strong>g you want to with the type anyway while it’s compil<strong>in</strong>g the codefor this particular set of template arguments. The C++ standards committee has recognizedthat constra<strong>in</strong>ts are still useful, though, and they will be present <strong>in</strong> C++0x (thenext version of C++) under the name of concepts.The C++ compiler is smart enough to compile the code only once for any given setof template arguments, but it isn’t able to share code <strong>in</strong> the way that the CLR does withLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!