In the previous discussion we had seen some of the important C# concepts such as Stack, Heap, Value Type, Reference Type, Concepts of Boxing & Unboxing, Regular Expressions or Regex, Indexers.

So now in this discussion we will be discussing some of the concepts which are simple but are not understood by many of them due to the confusion as there are many variations available in .NET framework.

Collections:

Now by looking at the word collection a general definition comes to our mind which is a group of people or group of things, products, etc. Now collection exactly means that a group of things or individuals which are having same properties and because of that they are grouped together. For e.g. if there are some songs or videos we call them as collection of songs or videos because they have the same property and therefore they are grouped together.

In the similar way we can define Collections in our C# .NET as group of records or properties which we can treat or confirm as one logical unit. For e.g. we have collections of the product code and product name, country code and country name.

Now as we have seen what is collection and we will now discuss what its different types are. So basically collections are of 4 types, they are:

  • Indexed Based.
  • Key Value Pair Based.
  • Prioritized Based.
  • Specialized Based.

We will now discuss each one of them in detail.

Indexed Based:

In indexed based collection there are two more sub types which are Array & List. Now indexed based collections are that type of collections where the values of any row or any row of the collection are obtained by the internal generated index. Now we see that every time a record is created in .NET collection it maintains its own internal index number and due to that the value of any row in the collection can be selected using that internal index number. For example if there are 10 records created the collection will generate internal index numbers from 0-9. For better understanding we take other example, here we consider country code and country name.

1     IND      INDIA

2     ENG     England

3     CHI       China

4     JAP       JAPAN

5     AUS      Australia

Here “IND” is the country code and “India” is the country name and internally index numbers are generated from 1-5. So now if we want to select any country we do it by selecting its index number. Therefore if we want to select “India”, we have to just write,

Array [1]    or   List [1]

If we want to select “Japan” we have to just write,

Array [4]   or   List [4]

So in short indexed based collection helps us to access the value of record through the internally auto generated index numbers.

Key Value Pair Based:

This Key value pair based collection is we can say other option for the index based collection. Because what happens is that in big programs or applications the collection of records are not small as we saw in the previous example, in big applications the collection of records are large and to display the value of a certain record we cannot depend on index based collection as we do not know what internal index number is generated for that particular record in the collection. And therefore we use key value pair based collection. There are 2 types of key value pair collections known as Hashtables&SortedList.

Now if we consider the same above example that is:

1     IND      INDIA

2     ENG     England

3     CHI       China

4     JAP       JAPAN

5     AUS      Australia

Here as discussed above in real projects or big projects we cannot access the record in a collection using indexed based and therefore we while creating the record assign some user defined key so that when we want any record we just have to put the key and we get the desired record. That is:

If we want “England” we have to write,

Hashtable (“ENG”)

So by just putting the user defined key we can access any record from the collection whether the collection is big or not.

Prioritized Collection:

This type of collection as the name says is based on the priorities, that is according to the priority the records in the collection are accessed. There are two types of Prioritized collection known as Stack & Queues.

Here the Stack follows the LIFO priority, that is Last in First Out. It means that if the records of the collection are in stack, then the last created record will be accessed first and the first created record will be accessed last. That is why it is Last in First out.

Also here the Queue follows the FIFO priority, that is First in  First out. It means that the records in the collection are accessed in the same order as they were created, that is the record created first will be accessed first and the record created last will be accessed last. For e.g. in real world when we stand in a queue the 1st person in the queue is allow to go first and the last person is allowed to go last.

So therefore the prioritized collection helps us to access the records in a collection according to the priority i.e. Stack & Queue.

Specialized Collection:

These are special types of collection or we can say out of the box type of collection which helps us to access the records for a very specific or special purpose. Therefore they are known as specialized collection. There are some few types of specialized collection such as String Collection, Hybrid Dictionary, etc.

Different types of string collection are used to access the string records. And the hybrid dictionary we can say that the whole collection starts initially as a List and then becomes as a Hashtables.

So therefore the specialized collections are used for a specific purpose to access the records, we can also say that they are special type of collection.

Now we will see some other important things such as what are arrays and arraylist? And what is the difference between them and also which is better at performance.

Arrays:

Arrays are a type of collection and it is defined by some of its properties. Its properties are:

  • Arrays are fixed in size
  • Arrays are strongly typed

ArrayList:

ArrayList are also a type of collection and has some properties. Its properties are:

  • ArrayList are resizable
  • ArrayList can store any data type

So now we see that what is the difference between Arrays &ArrayList and also in which scenarios we use arrays and arraylist.

Difference:

So basically there are two main differences between Arrays & ArrayList. They are:

  • Arrays are having fixed size whereas ArrayList are resizable. It is because in the real projects we want our data collection to be resizable as we do not know how much data will be coming from user side.
  • Arrays are strongly typed that is when we create an array it can store only one datatype, Whereas ArrayList can store any data type.

Now talking about the performance of Arrays & ArrayList we come to know which is better.

So as we know that ArrayList can store multiple datatype, due to this there is a lot of boxing & unboxing happening due to which it is time and memory consuming. Arrays are strongly typed and only store one datatype due to which boxing & unboxing does not happen. So we come to a conclusion that Arrays are faster in performance than ArrayList.

Also to overcome the limitations of Arrays like fixed size, Strongly typed and a complex code to fill the array, we use arraylist which is resizable, can store any datatype and very importantly instead of writing the complex code we have simple interfaces like add, remove to fill the arraylist. Also ArrayList have easy syntaxes.

ArrayList and Hashtables:

Now as we have understood the properties of arrays and arraylist and their difference, we come to know that there are some drawbacks or shortcomings in arraylist. And these shortcomings can be overcome by using Hashtables. Let us see how it is done.

As we know that ArrayList is indexed base type collection. And due to this when we want to find some record we have to create an object ArrayList and in that we have to provide with the index number which is internally generated. By providing that index number only we can find the record from the collection. But this becomes difficult because in the large collection we cannot remember the exact index number for our exact record. Therefore in this case we use to access the records using some keys. And that’s where Hashtables come to the rescue.

By creating the object of hashtable it allows us to write the values of the object and also write its specific key which will help to access the record easily.

But performance wise ArrayList are faster than the Hashtables because Hashtables perform the extra task of forming hashes which are created for the data. So if we want to access the records using key words we use Hashtables and if we want to just add records and browse through the collection we use ArrayList.

India          01

China         02

USA            03

Here the country names are data values and “01”, “02”, “03” are called as Hashes.

Special Collections:

Special collections are known as the customized collection in the .NET framework and they are used for the special occasions such as when we want to use String to access records we use String Collection or if we want to use integer to access records we use Integer Collection. So basically there are 4 important types of Special collections which are mostly used. They are:

  • CollectionsUtil:
    The main function of this is to create collections that are case insensitive in strings. It means that if we want to access a record and by writing the key in small or capital letters we will be able to find the record in the collection because this collection is case insensitive.
  • ListDictionary:
    This type of collection is good when it is used for collections which contain 10 or less records. For e.g. there are 7 days in a week and for this collection of days ListDictionary is used.
  • HybridDictionary:
    As the name is hybrid it uses the properties of two collections. As we know that ListDictionary is used when the records are 10 or less than 10, here if the records are increased the performance will decrease. Similarly it is totally opposite in Hashtables where the numbers of records are less due to which performance is not stable and if the number of records are increased the performance becomes stable. And therefore we use HybridDictionary, where if the number of records are less than or equal to 10 it uses ListDictionary and as the number of records are increased it automatically switches to hashtable collection.
  • StringCollection:
    It is used when we want to create a collection which will represent only strings. Therefore it is a special collection which is used only to store strings.

Now after understanding everything about the collections we will learn the concept of Generics.

Generics:

Here we can say that generic means generalization. Now we have a compare function where string and integer datatype are compared. Here the datatypes are different but the compare logic is the same. So if n a bigger application if there are more datatypes added then we want our code to have reusability factor. And that is where the role of generics comes.

Generics are defined as function which helps us to separate or decouple the main logic from the datatype to increase its reusability. In other we can say that we can create a different class where we can define the datatype on runtime.

We understand this by a simple code given below:

Class1<int>obj = new Class1<int>();

bool b = obj.compareme(1,2);

Class1<string>obj = new Class1<string>();

bool b = obj.compareme(“abc”, “abc”);

Here the datatype is defined during the runtime. Here we have Class1 which hascompareme logic. And the int and string datatype is defined in the”<>” sign.

As now we have decoupled the compare logic from the datatype we can now use any datatype during runtime and can reuse the compare logic as many times we want.

Now it happens that there are some logics which have some specific datatypes which are used. If we use other datatypes it will not work. For e.g. in compare logic we have greater than or less than or equal to logics and these logics work only on int, double datatypes. If we were to use string datatype it will give undesired results because we cannot compare string in greater than or less than manner. And therefore comes the concept of Generic Constraints.

Generic Constraints:

Here the generic type can be restricted by using “where” keyword. Here we have to define a specific datatype for the specific logic and then use the “where” keyword. Now after defining the specific datatype by using where keyword if we try to insert any other datatype it will not allow, thus it will avoid undesirable result.

public class CompareNumeric<unknowndatatype>

where<unknowndatatype>: int, double

{

}

 

 

 

Advertisements