C# For Programmers
I just breezed through Illustrated C# 2005, and made a list of language features / concepts that struck me for one reason or another, mostly as a function of being either different from (or similar to!) features in other languages I am already familiar with.
refandoutparameters- pass value objects into or “out of” methods by reference
- properties
- allow exposing properties as fields, backed by getter and/or setter methods
staticconstructors- where it is possible to initialize static fields
- finalizers
- where it is possible to clean up resources used by an instance. Syntax is reminiscent of syntax for C++ destructors.
- indexers
- allows the square bracket operator (i.e.
[5]) to be applied to instances - delegates
- essentially closures that invoke zero or more methods with the same signature. Event handlers are implemented as delegates, such that adding and removing listeners is accomplished using the
+=and-=operators. partialclasses- allow classes to be defined / extended across multiple files
usingstatements- a convenience construct that maps to a
try { ... } finally { ... }block, callingDispose()(fromIDisposable) on the used object if an exception is thrown - attributes
- essentially Java annotations
- inherited methods with
virtual,override,new - method overridding must be explicitly permitted with
virtual. Overriding and hiding also must be explicitly called out, viaoverrideandnew. - iterator blocks,
yield returnstatements - when defining new iterators for use in
foreachstatements - nullable value types,
??(null coalescing operator) - value types can be allowed to have the value
null. The null coalescing operator allows an alternative value to be specified more concisely, when a variable is found to benull. Similar to Groovy’s “elvis” operator?:. implicitandexplicittype conversions- conversions can be defined between types for which an inheritance relationship does not exist. Overall, this seems like a confusingly “clever” feature to use, and probably best avoided.
- generics and constraints (
whereclauses) - like Java generics, but with more expressive type constraints expressed as
whereclauses. is,as,typeof()- how to dynamically check type information;
isis like Java’sinstanceof;asis like a C++dynamic_cast; andtypeof()returns aTypeobject.