Section and page numbers refer to C# Precisely, MIT Press 2004:
sizeof(t)
expression is
now a compile-time constant (and safe code) when type
t
is a one of the simple types sbyte, byte, short,
ushort, int, uint, long, ulong, char, float, double, bool, and
decimal. Its value is the size in bytes given in the table on page
7.
x
should be res
.
t?
cannot be made any
more nullable: the types t??
, t???
and so
on are now illegal. Also, the boxing of a null
nullable value produces a null
reference; and boxing
of a non-null
nullable value produces a boxed version
of the value contained in the nullable. Finally,
bool?
type arguments cannot be used in the statements
for, if, while, do-while, nor in the operators (&&), (||)
and (?:).
T :
struct
now means that T
must be a value type
and must be non-nullable.
System.Collections.Generic.IEnumerator<T>
now
extends the non-generic interface
System.Collections.IEnumerator
, and its
implementations must therefore look like this:
class MyEnumerator : IEnumerator<T> { // These implement IEnumerator<T>: public T Current { get { ... } } public void Dispose() { ... } // These implement IEnumerator: Object IEnumerator.Current { get { return Current; } } public void Reset() { ... } // This one implements both: public bool MoveNext() { ... } }Fortunately, this follows automatically if you implement the enumerator using an iterator block (C# Precisely section 13.12).
System.Collections.Generic.IEnumerable<T>
now
extends the non-generic interface
System.Collections.IEnumerable
, and its
implementations should therefore look like this:
class MyEnumerable : IEnumerable<T> { public IEnumerator<T> GetEnumerator() { ... } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }Method
IEnumerable.GetEnumerator()
is a so-called
explicit interface member implementation. It calls the other
GetEnumerator()
method, whose result has type
System.Collections.Generic.IEnumerator<T>
, which is
a subtype of System.Collections.IEnumerator
as required.
Note that access modifiers such as public
are not allowed
on explicit interface member implementations.
If you forget to implement method
IEnumerable.GetEnumerator()
, you typically get a compiler
error CS0535 or CS0536 with this message: Class does not
implement interface member
System.Collections.IEnumerable.GetEnumerator()
.
System.IComparable<T>
has been split into two
interfaces, both in namespace System
:
interface IComparable<T> { int CompareTo(T y); } interface IEquatable<T> { bool Equals(T y); }Type
int
implements IComparable<int>
and IEquatable<int>
, and similarly for all other
simple types and for String
.
System.Collections.Generic.IComparer<T>
has been
split into two interfaces, both in namespace
System.Collections.Generic
:
interface IComparer<T> { int Compare(T x, T y); } interface IEqualityComparer<T> { bool Equals(T x, T y); int GetHashCode(T x); }