IntroductionThe C# 3.0 has lots of new features and new ways of object initialization. In this snippet I will show how to Initialize Dictionary Collection object with new feature using C# 3.0. First i will show how is we are initialize the dictionary collection object in old version of the C# and then how is new version. Old Version C# Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class DictionarySample
{
private Dictionary<int, string> OldDictionary()
{
Dictionary<int, string> collectionNames = new Dictionary<int, string>();
collectionNames.Add(1, "RRaveen");
collectionNames.Add(2, "Dhan");
collectionNames.Add(3, "Abhi");
collectionNames.Add(4, "Jana");
return collectionNames;
}
}
}
New Version C# Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class DictionarySample
{
private Dictionary<int, string> NewDictionary()
{
Dictionary<int, string> collectionNames = new Dictionary<int, string>()
{
{1, "RRaveen"},
{2, "Dhan"},
{3, "Abhi"},
{4, "Jana"}
};
return collectionNames;
}
}
}
The new version is easy and less line code.so writes code always using new features in .NET Framework. Hopes help. |