Monday, June 6, 2011

Lookup in .Net 3.5 and above

If you want to use a Dictionary type of collection with duplicate key values you can go with Lookup

more details read: http://msdn.microsoft.com/en-us/library/bb460184.aspx

Wednesday, May 25, 2011

Wednesday, May 18, 2011

Lazy in C#

If you want to initialize a class as lazy Initialization you can go with Lazy key word.

Lazy _expensive = new Lazy
(() => new Expensive(), true);

Note: If you pass true as parameter,it will be thread safe else as false, it will not be thread safe.

If for a single -thread or single ton class Lazy is best

If you want to return a query with Default if its Empty?

If you want to return a query with default value if it is empty you can go with DefaultIfEmpty

void Main()
{
Coder coder = new Coder("Jon");

List coderCollection = new List()
{
new Coder("Anish"),
new Coder("marokey"),
};

var result = coderCollection.Where(r => r.Name == "Pat").DefaultIfEmpty(coder);

}

// Define other methods and classes here
class Coder
{
public string Name;

public Coder(string Name)
{
this.Name = Name;
}
}

Order by with out Culture

If you want to do some operations without any culture you can do with the help of StringCompare.Ordinal or StringCompare.OrdinalIgnoreCase.

void Main()
{
string[] nonCulture = { "äax", "ääü", "äbü" };

IEnumerable orderNonCulture = nonCulture.OrderBy( r => r, StringComparer.Ordinal);

orderNonCulture.Dump();
}