Posts

Showing posts from August, 2012

Constructor Vs Static Constructor

A Constructor is usually used to initialize data. However Static Constructor is used to initialize only static members. Here I am just talking about the Constructors. How they get initialized and how they behave. Things to know about Static Constructor It is used to initialize static data members.  Can't access anything but static members. Can't have parameters Can't have access modifiers like Public, Private or Protected. Now once you understand the above points, you can appreciate the difference between Static Class and Unstatic Class Static Class cannot be instantiated unlike the unstatic class. You should directly access its Method via the ClassName.MethodName A Program can't tell when it is going to load static class but its definitely loaded before the call. A Static class will always have the static constructor and its called only once since after that its in the memory for its lifetime. A Static class can contain only static members. So all the

Lambda Expressions

Lambda expressions are part of .Net 3.0. They are used extensively in Linq, but they can also be used on their own. With Lambda expressions, filtering and sorting Lists has become a lot easier.  I'm going to filter and sort Lists of Employee objects: public class Employee {      public string FirstName { set ; get ;}      public string LastName { set ; get ;}      public decimal Salary { set ; get ;}      public bool IsManager { set ; get ;} } FindAll( )   Suppose I have a List of Employees, and I want to find all the managers.  At one point I would have written code like this: List < Employee > managers = new List< Employee >( ); foreach ( Employee employee in employees) {      if (employee.IsManager == true )         managers.Add(employee); } The new syntax with Lambda expressions is clean and simple:   List < Employee > managers = employees.FindAll(employee => employee.IsManager == true ); Note that the term "employee