Introduction of Collections in C#


In this article I explain about what is Collections in C# That is , Collections are the most essential component of C Sharp(C#). The C# language provides the features necessary to create your own collections.

Introduction of Collections in C


A collection is a set of objects. Simply, collection represents a set of objects that you simply will access by stepping through every component successively. Basically A collection is a class, so you must have declared a new collection before you may add elements to that collection. The .NET Framework provides specialized categories for managing collection and these categories have wealthy capability for enhancing your programming expertise through higher performance and easy maintenance.
The principal advantages of collections are that they standardize the manner teams of objects are handled by your programs. All collections area unit designed around a set of cleanly outlined interfaces. many inherent implementations of those interfaces, like Array List, Hashtables, Stack and Queue etc, area unit provided, that you'll use conjointly implement your own collection, however you may rarely ought to.
The following types of collection classes are described in below section:
· System.Collections.Concurrent classes
· System.Collections.Generic classes
· System.Collections classes
· Visual Basic Collection class
. The .Net framework contains a large variety of interfaces and categories that outline and implement varied types of assortment. Object category is that the base category of each type in .NET. All the collections implement IEnumerable interface that's extended by ICollection interface. IDictionary and IList are interfaces for collection that are derived from ICollection as shown in below diagram.
Diagram of Collections
System.ICollection.IEnumerable:
It exposes about the enumerator, which provides a collection, according to user defined classes.
Methods:
GetEnumerator ():It returns the official object which will be accustomed restate through the gathering. It permits mistreatment the foreach statement. Enumerators solely enable reading the information within the collection.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
 namespace CollectionExampleDemo
{ class Program
{
static void Main(string[] args)
{
Array arry = new string[] { "Mohan", "Milan", "Cooler", "Pawan", "Ketan", "Mike" };
IEnumerator ienum = arry.GetEnumerator();
 
string enumMsg = ""; while(ienum.MoveNext())
{
string value = (string)ienum.Current;
enumMsg += value + "\n";
}
Console.Write(enumMsg);
Console.ReadKey();
}
}
}

 
 
System.Collections.ICollection:
ICollection interface specifies a technique for obtaining the dimensions of assortment, making enumerators on a collection and managing synchronal access to any or all non-generic collections. it's a base interface for categories within the System.Collections namespace.
Briefly, the fundamental variations between Generic assortment and Non-Generic Collection:
Non-Generic collections - These area unit the collections which will hold parts of various knowledge sorts. It holds all parts as object kind. Thus it includes overhead of kind conversions.
Some advantages of generic collections - Type Safe, Secure, reduced overhead of type conversions.

Important Properties: Count: It returns the number of items of contain by the ICollection.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
 
namespace CollectionExampleDemo
{
class Program
{
static void Main(string[] args)
{
ArrayList arryList = new ArrayList();
arryList.Add("Milk");
arryList.Add("Water");
arryList.Add("Caca");
arryList.Add("Votka");
 
Console.Write("Collection items count: {0}",arryList.Count); // it will return count 4
Console.ReadKey();
}
}
}

 
 
 
Code Clarification: In above example declaring dynamic array and added four string object such as Milk, Water, Caca, Votka etc, Count is property of ICollection interface which returns number of items in collection.
IsSynchronized: It returns true if access to the ICollection is synchronized.
SyncRoot: It returns an object that can be used to synchronize access to the ICollection.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
 
namespace CollectionExampleTestDemo
{
class Program
{
static void Main(string[] args)
{
ArrayList arryList = new ArrayList();
arryList.Add("Milk");
arryList.Add("Water");
arryList.Add("Caca");
arryList.Add("Votka");
 
 
lock (arryList.SyncRoot)
{
string list = string.Empty;
foreach (var lst in arryList)
{
if (list.Length > 0)
list += ",";
list += lst.ToString();
}
Console.Write(list);
Console.ReadKey();
}
}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: