Posts

Showing posts from February, 2015

Event Initializer in C# 6.0

  Take the following sample class which has a property and an event listener public class Lecturer      {          public string Name { get ; set ; }          public EventHandler < EventArgs > Speaking;        } . In C# 6.0 when you initialize a new object wire up delegate to the event inside the object initialization syntax. Before C# 6.0 it was illegal. static void Main( string [] args)        {            EventHandler < EventArgs > log = (e, o) => Console .WriteLine( "Speaking" );              Lecturer mathsLecturer = new Lecturer            {                Name = "Perera" ,                Speaking += log            };        } Also you can directly assign the lambda to the event as follows. static void Main( string [] args)          {              EventHandler < EventArgs > log = (e, o) => Console .WriteLine( "Speaking" );                Lecturer math...

Dictionary Initializer in C# 6.0

  After C# 3.0 is released we had the ability of initializing lists and dictionaries using following syntax. Dictionary < string , User > userList = new Dictionary < string , User >              {                  { "admin" , new User ( "Sammani" ) },                    { "Guest" , new User ( "Hemal" ) },              }; Here we add the key value pairs inside the “{ }”. In c# 6.0 it has a new syntax as follows which gives more look like working with key value pairs. Dictionary < string , User > userList = new Dictionary < string , User >            {                [ "admin" ]= new User ( "Sammani" ) ,                 [ "Guest" ]= new User ( "Hemal" )            }; you can the number of characters are almost same in both the syntax but in c# 6.0 syntax looks more a like key value pair. This is another syntax sugar added in c# 6.0. Happy Codi...