Sunday, 6 October 2013

Collections in C#

Collections are group of records which can be treated as single logical unit. Collection classes are specialized classes for data storage and retrieval.Collection classes are derived from System.Collection namespace
Dot net  collections are divided into four categories:-
1. Indexed based
Indexed based collections helps to access the value of the row using internally generated index by the collections. They are two types:-
Array
List
2. Key value pair
Key value pair based collections helps to access the value of the row using user defined key. They are two types:-
Hashtable: - Uses a key to access elements in the collections
Sorted list: - Uses a key as well as an index to access elements in a list. It is a combination of an array and a hashtable.If we access items using an index, it is an arraylist and if we access the items using a key, it is a hashtable. The collection of items is always sorted by the key valu e.
3. Prioritized collections
Stacks
Queue
Queues and stacks helps to access data in a particular sequence.
Queues uses FIFO(First in First out) methodology.
Stacks uses LIFO(Last in First out) methodology.
4. Specialized collection
String collections

Hybrid dictionary

Thursday, 3 October 2013

Lamda Expression in C#

The concept of lamda expression was introduced in C# 3.0. It is just a new way to write anonymous methods. At compile time all the lamda expressions are converted into anonymous methods according to lamda expression conversion rules. The left side of the lamda operator "=>"(is called ‘goes to’ operator) represents the arguments to the method and the right side is the method body.

Lambda expression Syntax:
(parameters) => expression-or-statement-block

Types of Lamda expression

1. Statement Lamda
Statement lambda has a statement block on the right side of the lambda operator "=>".
Eg: a => {return a * a;};

       
2. Expression Lamda
Expression lambda has only an expression .Will not have a return statement or curly braces, on the right side of the lambda
operator "=>".
Eg: a => a * a; // here a*a is expression


Lets take an example :

Lets convert it to lambda expression

X=>x+2 is the lambda expression. The left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block.

Hope you are enjoyed this.