Saturday, September 15, 2012

Enumerable.Range

To loop through a sequence of numbers we can use Enumerable.Range static method.

var x= (from i in Enumerable.Range(0,12)

select i).ToArray();

Above code will create array of integers with values from 0 to 11.

 

Note:

  • First argument is start index
  • Second argument is Count

Thursday, September 13, 2012

Dictionary- An item with the same key has already been added.

If you try to add same key to a Dictionary you may encounter this error

var ht = new Dictionary<String,String>();
ht.Add("key", "Data1");
.
.
.
ht.Add("key", "Data2");

To avoid this use you can simply change this code like

var ht = new Dictionary<String,String>();
ht["key"] = "Data1";
.
.
.
ht["key"] = "Data2";

This indexer will add the key, if it is not in the Dictionary, else will just update the value

Item has already been added. Key in dictionary: 'key' Key being added: 'key'

If you try to add same key to a hashtable you may encounter this error

var ht = new Hashtable();
ht.Add("key", "Data1");
.
.
.
ht.Add("key", "Data2");

To avoid this use you can simply change this code like


var ht = new Hashtable();
ht["key"] = "Data1";
.
.
.
ht["key"] = "Data2";

This index will add the key, if it is not in the hashtable, else will just update the value

Wednesday, September 12, 2012

Extension method must be static

Compilation Error:

Extension method must be static

Cause.

Extension methods (with this keyword in the first parameter), should be marked as static. Non static methods cannot be extension methods.

Collection classes

Following classes we can use for storing collection of Data.

  • System.Array
    • To store same type of data
    • Specific size
    • Direct Access
  • System.Collections.ArrayList
    • To store different types of data
    • Variable size
    • Direct Access
  • System.Collections.Generic.List<T>
    • To store specific types of data
    • Variable Size
    • Direct Access
  • System.Collections.Queue
    • To store different types of data.
    • Variable Size
    • First In First Out data access
  • System.Collections.Generic.Queue<T>
    • To store specific types of data.
    • Variable Size
    • First In First Out data access
  • System.Collections.Stack
    • To store different types of data.
    • Variable size
    • Last In First Out data access
  • System.Collections.Generic.Stack<T>
    • To store specific types of data.
    • Variable size
    • Last In First Out data access
  • System.Collections.Generic.HashSet<T>
    • Generic type
    • To store specific types of data
    • Variable Size
    • Direct Access
    • Supports Set operations such as Intersect, Union

Wednesday, September 05, 2012

The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

If you encounter this compilation error, it means, you are trying var keyword outside a code block. var works only within code blocks, such as methods and property get and set.