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.

ref and out parameters
pass value objects into or “out of” methods by reference
properties
allow exposing properties as fields, backed by getter and/or setter methods
static constructors
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.
partial classes
allow classes to be defined / extended across multiple files
using statements
a convenience construct that maps to a try { ... } finally { ... } block, calling Dispose() (from IDisposable) 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, via override and new.
iterator blocks, yield return statements
when defining new iterators for use in foreach statements
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 be null. Similar to Groovy’s “elvis” operator ?: .
implicit and explicit type 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 (where clauses)
like Java generics, but with more expressive type constraints expressed as where clauses.
is, as, typeof()
how to dynamically check type information; is is like Java’s instanceof; as is like a C++ dynamic_cast; and typeof() returns a Type object.

Leave a Reply


Bad Behavior has blocked 94 access attempts in the last 7 days.