Powered by Blogger.

Sunday, February 23, 2014

Collections in C#.Net



Collections are similar to arrays in that they enable you to store more than one object in a single variable. The same bag analogy works for collections: you can simply drop a number of items in a bag, and it will hold them for you.

Also check out:

- sys.objects type in SQL Server 2008

- Format Strings in javascript

- Show confirmation message in gridview for delete in Asp.net

Most collections expose an Add method that enables you to add an item to the collection.They have Remove and Clear methods to remove one or all items from the collection.
Collections enable you to iterate, or loop, over them to access the items in it like arrays.
The ArrayList and Hashtable are the type of collections became popular very quickly because they were so easy to use.

The ArrayList enables you to add arbitrary objects that are then stored in the order you add them, whereas the Hashtable enables you to store objects referenced by a custom key.

The main benefit of these collections over their array is that they can grow on demand and the ArrayList grows dynamically when required like the below examples.

VB.NET
Dim member As New ArrayList() ‘ Create a new ArrayList. You don’t need ‘ to set its size explicitly
member.Add(“Biju”) ‘ Add the first member
member.Add(“Raj”) ‘ Add the second member
member.Add(“Tamanna”) ‘ Keep adding member and the ArrayList grows as necessary

C#.Net
ArrayList member = new ArrayList(); // Create a new ArrayList. You don’t need to set its size explicitly
member.Add(“Biju”); // Add the first member
member.Add(“Raj”); // Add the second member
member.Add(“Tamanna”); // Keep adding member and the ArrayList // grows as necessary

The above now calls a method (Add) rather than assigning an item to a predefined index in an array, you need parentheses (()) in both VB.NET and C#.

The biggest drawback of the ArrayList is that it isn’t strongly typed. What this means
is that you can add any object to the list using the Add method. This means that the ArrayList could hold objects that are of different types at the same time.

In .NET 2.0, Microsoft introduced a concept called generics. Generics are still strongly present in version 4 of .NET, helping you overcome the problems that weakly typed collections like the ArrayList introduced.

Generics
Generics are to code what Microsoft Word templates are to word processing. They enable you to write a code template that can be used in different scenarios with different types. With generics, you can define a generic code template that doesn’t explicitly specify a type. Only when that code is used do you define the type. The main benefit of this is that you can reuse the same template over and over again for multiple data types, without retyping and maintaining multiple versions of the code.Follow the below examples.

VB.NET
Dim member As New List(Of String)
member.Add(“Biju”)
member.Add(“Raj”)
member.Add(“Tamanna”)

C#.Net
List<string> member = new List<string>();
member.Add(“Biju”);
member.Add(“Raj”);
member.Add(“Tamanna”);

Similar to a generics list of strings, you can also create lists to hold other types. For example:

VB.NET
Dim intList As New List(Of Integer) ‘ Can hold Integers only
Dim boolList As New List(Of Boolean) ‘ Can hold Booleans only
Dim buttonList As New List (Of Button) ‘ Can hold Button controls only

C#.NET
List<int> intList = new List<int>(); // Can hold ints only
List<bool> boolList = new List<bool>(); // Can hold bools only
List<Button> buttonList = new List<Button>(); // Can hold Button controls only

With a collection initializer, you declare the collection, and add some items in one
step.
VB.NET
Dim myList As New List(Of Integer) From {1, 2, 3, 4, 5,6}

C#.NET
List<int> myList = new List<int>() { 1, 2, 3, 4, 5,6 };





0 comments

Post a Comment