When to use collectins and generics in c# [Resolved]

Posted by Prajakta_2701 under C# on 9/20/2013 | Points: 10 | Views : 2431 | Status : [Member] | Replies : 5
When to use collectins and generics in c#?




Responses

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Generics provides the type safe code with re-usability like as algorithm. In algorithms such as sorting, searching, comparing etc. you don’t specify what data type(s) the algorithm operates on. The algorithm can be operates with any types of data. In the same way Generics operate, you can provide different data type to Generics. For example, a sorting algorithm can operates on integer type, decimal type, string type, DateTime type etc.

Following are the main advantage of Generics.
Code Re-usability with Generics :
Type Safety with Generics

Example
Suppose, you required to sort the integer and floating type numbers, then let's see how to do in collections and generics.
How to do it using Collections
//Overloaded sort methods
private int[] Sort(int[] inputArray){

//Sort array
//and return sorted array
return inputArray;
}

private float[] Sort(float[] inputArray)
{
//Sort array
//and return sorted array
return inputArray;
}


How to do it using Generics
private T[] Sort(T[] inputArray){ 

//Sort array
//and return sorted array
return inputArray;
}


Reference:
http://www.dotnet-tricks.com/Tutorial/csharp/U08E301212-Difference-between-Generics-and-Collections-with-example.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Prajakta_2701, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You see the below points:-

1. Use generic types to maximize code reuse, type safety, and performance.
2. The most common use of generics is to create collection classes.
3. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace.
4. These should be used whenever possible in place of classes such as ArrayList in the System.Collections namespace.
5. You can create your own generic interfaces, classes, methods, events and delegates.
6. Generic classes may be constrained to enable access to methods on particular data types.
7. Information on the types used in a generic data type may be obtained at run-time by means of reflection.

Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Prajakta_2701, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Various Collection Classes and when to use them

ArrayList
Use this if you want to represents ordered collection of an object that can be indexed individually.
It is basically an alternative to an array. However unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, add, search and sort items in the list.

Hashtable
Use this if you want to uses a key to access the elements in the collection.
A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

SortedList
Use this if you want to uses a key as well as an index to access the items in a list.
A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key , it is a Hashtable. The collection of items is always sorted by the key value.

Stack
Use this if you want to represents a last-in, first out collection of object.
It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.

Queue
Use this if you want to represents a first-in, first out collection of object.
It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue and when you remove an item, it is called deque.

BitArray
Use this if you want to represents an array of the binary representation using the values 1 and 0.
It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index, which starts from zero.

Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Prajakta_2701, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Other users:-

1. In collections, Each and every items are boxed and stored as a object internally. Performance of arraylist for retrieving and storing will be slower.
2. In generics, boxing and unboxing is not required when storing and retrieving items from the generic class.
3. Generic collections - These are the collections that can hold data of same type and we can decide while initializing what type of data that collections can hold. Advantages - Type Safe, Secure, reduced overhead of implicit and explicit conversions.
4. Collections hold elements of different datatypes, it hold all elements as object type. so it includes overhead of implicit and explicit conversions. There can be runtime errors

See the below images:-

Generics - http://questpond.blog.com/files/2011/05/1a9.jpg
Collectins - http://1.bp.blogspot.com/-lB3ZdjTVJVI/UXu68kZkNgI/AAAAAAAAABU/6jncb9y91cM/s1600/Collection.png

Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Prajakta_2701, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Collections provide a more flexible way to work with groups of objects. Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the application change. For some collections, you can assign a key to any object that you put into the collection so that you can quickly retrieve the object by using the key.

A collection is a class, so you must declare a new collection before you can add elements to that collection.

If your collection contains elements of only one data type, you can use one of the classes in the System.Collections.Generic namespace. A generic collection enforces type safety so that no other data type can be added to it. When you retrieve an element from a generic collection, you do not have to determine its data type or convert it.

Difference:
the basic difference between generic and non-generic collections:

- Non-Generic collections - These are the collections that can hold elements of different data types. It holds all elements as object type.
So it includes overhead of type conversions.

- Generic collections - These are the collections that can hold data of same type and we can decide what type of data that collections can hold.

Some advantages of generic collections - Type Safe, Secure, reduced overhead of type conversions.

Look into http://msdn.microsoft.com/en-us/library/ms379564%28VS.80%29.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Prajakta_2701, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response