Showing posts from June, 2009

C# New features

I have move to .net 3.5 and found really cool enhancements in C# language(in VB.Net too).
We can create properties like this
class ProcessData { public int ID { get; set; } public string Name { get; set; } public long Memory { get; set; } }
Compiler will emits the private variables for properties and nothing diffrent in IL code.

Object Initializer
Lets say we want to populate list of ProcessData class, before C# 3.0 came we have to code like this
List processList = new List(); foreach (Process p in Process.GetProcesses()) { ProcessData pd = new ProcessData(); pd.ID = p.Id; pd.Name = p.ProcessName; pd.Memory = p.WorkingSet64; processList.Add(pd); }
Note its IL code,
IL_001f: ldloc.2 IL_0020: ldloc.1 IL_0021: callvirt instance int32 [System]System.Diagnostics.Process::get_Id() IL_0026: callvirt instance void NewFeatures.ProcessData::set_ID(int32) IL_002b: nop IL_002c: ldloc.2 IL_002d: ldloc.1 IL_002e: callvirt instance string [System]System.Diagnostics.Process::get_ProcessName() IL_0033: c…