I'd like to learn what are all the convenience features of C#, and how they map to C#.
For example, automatic properties:
public string Foo { get; set; }
...maps to something like this:
string <Foo>k__BackingField;
[CompilerGenerated]
public string Foo {
get { return this.<Foo>k__BackingField; }
set { this.<Foo>k__BackingField = value; }
}
Foreach loops:
foreach(char c in "Hello") {
Console.WriteLine(c);
}
...maps to something like this (I think):
CharEnumerator en;
try {
en = "Hello".GetEnumerator();
while (en.MoveNext()) {
char c = en.Current;
Console.WriteLine(c);
}
} finally {
IDisposable disp = en as IDisposable;
if (disp != null)
disp.Dispose();
}
The disposing of the enumerator makes foreach
very useful when dealing with unmanaged resources, like looping through lines in a file, or records in a database.
I think a good understanding of these high level features can help us write better code. What are other convenience features of C#, and how do they map to C#?