C# to C#, convenience language features
.net, c#
Solution
If you want to talk about the amount of code saved, the biggest savers (IMO) are:
iterator blocks
Example:
public static IEnumerable<int> Get() {yield return 1; yield return 2;}
Saved:
- writing an `IEnumerable<T>` implementation
- writing an `IEnumerator<T>` implementation
- handling state of the enumerator; have you seen how horrible it is? Seriously.
captured variables (anon methods / lambdas)
Example:
var list2 = list1.FindAll(x => (x.Value % 10) == find);
Saved:
- writing a class to hold the state
- in complex cases, nested state
- initializing the state wrapper
- writing a method on that class
Expression lambda compiler
Example (from above):
Expression<Func<Foo,bool>> pred = x => (x.Value % 10) == find;
Saved:
- yeuch - lots of tree code; very, very complex; in the above I can see (in no particular order) `Expression.Parameter`, `Expression.Property`, `Expression.Field`, `Expression.Constant`, `Expression.Equal`, `Expression.Modulo`, `Expression.Lambda`... combine ;-p
Problem
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#?