6 March 2013

What are Collections in .NET

List
First, the List type provides you with an efficient and dynamically-allocated array. It does not provide fast lookup in the general case, which you will want to use Dictionary for. It is excellent when used in loops.
[sourcecode language="csharp"]
// Use the List type.
List<string> list = new List<string>();
list.Add("cat");
list.Add("dog");
foreach (string element in list)
{
Console.WriteLine(element);
}
[/sourcecode]
Dictionary
The Dictionary type in the base class library is one of the most important ones you need to use for your C# programs. It is an implementation of a hashtable, which is an extremely efficient way to store keys for lookup. The Dictionary in .NET is well-designed.
[sourcecode language="csharp"]
// Use the dictionary.
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("cat", 1);
dict.Add("dog", 4);
Console.WriteLine(dict["cat"]);
Console.WriteLine(dict["dog"]);
[/sourcecode]
ArrayList
As shown in this program, the ArrayList is a collection found in System.Collections and it can store objects of any derived type. You don’t need to worry about the type of the elements, at least until you need to know their types to use them.
[sourcecode language="csharp"]
ArrayList list = new ArrayList();
list.Add("cat");
list.Add(2);
list.Add(false);
foreach (var element in list)
{
Console.WriteLine(element);
}
[/sourcecode]
Hashtable
Hashtable optimizes lookups. This type is used in the C# language. It computes a hash of each key you add. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the generic Dictionary type. First, you can create a new Hashtable with the simplest constructor. When it is created, the Hashtable has no values. We directly assign values with the indexer, which uses the square brackets [ ]. The example adds three integer keys with one string value each.
[sourcecode language="csharp"]
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
[/sourcecode]
Stack
Stack is a LIFO collection. It provides a powerful and simple last-in-first-out data structure. This can help you develop parsers quickly and also replace complex recursive algorithms. Stack is a generic type.
LIFO:
The last element added (with Push) to Stack is the first one removed (with Pop).
[sourcecode language="csharp"] Stack<int> stack = new Stack<int>();
stack.Push(100);
stack.Push(1000);
stack.Push(10000);
return stack;

[/sourcecode]
Queues
Queue is a FIFO collection. It helps when you have elements that you need to process in a first-in, first-out order. It processes the elements that you received longest ago first. We look at several examples of using Queue<T>. We think about some of the logic used for processing help requests in a C# application.
First-In-First-Out:
The queue data structure implements this algorithm. Queue is a generic type with one type parameter.
[sourcecode language="csharp"]
// New Queue of integers
Queue<int> q = new Queue<int>();
q.Enqueue(5); // Add 5 to the end of the Queue.
q.Enqueue(10); // Then add 10. 5 is at the start.
q.Enqueue(15); // Then add 15.
q.Enqueue(20); // Then add 20.
[/sourcecode]

What is the difference between arrays and collection?


Array:
  1. You need to specify the size of an array at the time of its declaration. It cannot be resized dynamically.
  2. The members of an array should be of the same data type.

Collection:
  1. The size of a collection can be adjusted dynamically, as per the user's requirement. It does not have fixed size.
  2. Collection can have elements of different types.

What is difference between overloading and overriding?

Overloading and Overriding both are example of polymorphism. Overriding is dynamic polymorphism while overloading is static polymorphism.
In other words, Overloading is evaluated at compile time whereas Overriding is evaluated at Run Time.
Overloading: Overloading is a mechanism by which we can have two or more different methods using same name.
Overloading can be implemented using different parameter data types, different number of parameters, and different order of parameters
Overriding: Overriding is a changing of behavior of a class in drive class using dynamic polymorphism. For example in C# you have a class A and another class B derives from A.  Class A have a virtual method abc and in class B using override you given new functionality to this method.


Dataset

DataSet : Dataset is used to store the data retrieved from database by dataadapter and make it available for .net application.
To fill data in to dataset fill() method of dataadapter is used and has the following syntax.
Da.Fill(Ds,”TableName”); 

When fill method was called, dataadapter will open a connection to database, executes select command, stores the data retrieved by select command in to dataset and immediately closes the connection. 

As connection to database was closed, any changes to the data in dataset will not be directly sent to the database and will be made only in the dataset. To send changes made to data in dataset to the database, Update() method of the dataadapter is used that has the following syntax.
Da.Update(Ds,”Tablename”); 

When Update method was called, dataadapter will again open the connection to database, executes insert, update and delete commands to send changes in dataset to database and immediately closes the connection. As connection is opened only when it is required and will be automatically closed when it was not required, this architecture is called disconnected architecture. 

A dataset can contain data in multiple tables. 

Self - Join in SQL Server

self-join is simply a normal SQL join that joins one table to itself. 
This is accomplished by using table name aliases to give each instance of the table a separate name.

When Useful?Joining a table to itself can be useful when you want to compare values in a column to 
other values in the same column.

We have sample records in Employee table.


Inner Join Example----

SELECT e1.Name EmployeeNamee2.name AS ManagerNameFROM Employee e1
INNER JOIN Employee e2ON e1.ManagerID e2.EmployeeID

Outer Join example---

SELECT e1.Name EmployeeNameISNULL(e2.name'Top Manager'ASManagerNameFROM Employee e1
LEFT JOIN Employee e2ON e1.ManagerID e2.EmployeeID