Initialize Dictionary Collection Using New Features in C# 3.0

No.of Views1080
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  25 Dec 2010 22:12:24
Tag : CSharp , CSharp4.0
The 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
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

The 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.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top