C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




doubts abt collections in C#.Net


Posted Date: 05 Sep 2008      Total Responses: 1

Posted By: karthick       Member Level: Silver     Points: 1



could anyone explain me with simple examples of collection in dot net


Generics and non-generics collections

syntax of IDictinary

waiting for the reply

thanks in advance





Responses

Author: mythili d    05 Sep 2008Member Level: SilverRating:     Points: 3

collections are like the arrays. The only difference being they can hold not all same type of data
for example an array list can hold int,char,string float etc all at the same time. In arrays if you wanted to create asuch a structure you will hhave to create different arrays with all different types.
a1 hold int a2 to hold char a3 for strings a4 float etc.
But in this kind of code there could be runtime problems like you could have imagined at runtime
because of the type mismatch. so in the later versions of .net you can use array list and other types of generic
collections to hold objects of all same type.

There are two types of collection like you mentioned the generic and the non generic type.

Generic collection:
To explain this I would like to explain the array first. In arrays iif the array is declared to be of int then you can
use the same array variable to store the array size number of integers. all refernce by the same name.
Similarly in the array list we can use to store the user created objects. for example employee.
eg
Daemployee emp= new Daemployee();
List<emp> mylistemp=List<emp>;

//for retrieving you could use
for each ( emp in mylistemp)
{
//to process code here
}

but however there could be problems when you have two diff type of objects and they have everything common like employee (id,firstname.lastname)
Person(id,firstname,lastname) You could use this list to store either a employee or the person. And there is still a problem with data because it is not all of asame type.
This is a human error in coding and has got nothing to do with the way these are define.
Besides using a array list there is no other easy way to store an array of object to process them.

The second type is the Dictionary whihc could store the name and value in the same variable name . This works exactly like the hastables. With has an advantage over
because they have type safety etc. because dictionary is a generic collection. where as hashtables are not.

DataRow jdeItemRow = DAItem.GetItemForJDEByID(itemID);
System.Collections.Generic.Dictionary<string, object> vals = new Dictionary<string, object>();

vals.Add("StyleNum", jdeItemRow["StyleNum"].ToString());
vals.Add("Division", jdeItemRow["Division"].ToString());
vals.Add("Category", jdeItemRow["Category"].ToString());
InsertRow(vals, "tblJDELiveExport");


So that is the example from my code. This the simplest way we could have used the dictionary collection.

I have not used the Hashtables a lot so cannot talk about them. Hope this made it easier for you to study more about these collections.
thanks



Post Reply
You must Sign In to post a response.
Next : remote file system
Previous : Change text color programatically in C#
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use