Friday, August 20, 2010

C# - What I learned newly over the years

Mutable & Immutable - String is a muttable(whenever we alter a string, new string object created) and StringBuilder is Immutable

Generics - This is the best one that introduced with 2.0 and it helps to maximize code reuse, type safety, and performance. It is commonly used for collection purpose. It is in System.Collections.Generic. So better to avoid classes present in System.Collections (Arraly List and hastable). Know about Generics more visit here

HashTable & Dictionary - These 2 objects holds key-value pair.The Dictionary class has the same functionality as the Hashtable class. A Dictionary of a specific type (other than Object) has better performance than a Hashtable for value types because the elements of Hashtable are of type Object and, therefore, boxing and unboxing typically occur if storing or retrieving a value type.

Partial Class - It helps us to have the same class in multiple files. This is very useful when multiple persons are working in same class and to differentiate definition & implementation in different files.

Using - This is good feature for GC

Type Casting and Comparison Using "as" and "is" -
DataGridItem item = mycontrol.Parent as DataGridItem;
if (item is DataGridItem){ }


Extension Methods
static class ExtensionSample
{
public static decimal Triple( this decimal d ) { return d*3; }
}


LINQ

It is efficient when we use with objects and dataset. for example to randomly order a list, list = list.OrderBy(emp => Guid.NewGuid()).ToList();

No comments:

Post a Comment