Monday, January 30, 2012

Anonymous Functions

An anonymous function is an "inline" statement or expression that can be used wherever a delegate type is expected. You can use it to initialize a named delegate or pass it instead of a named delegate type as a method parameter.

In C# 1.0, you created an instance of a delegate by explicitly initializing it with a method that was defined elsewhere in the code. C# 2.0 introduced the concept of anonymous methods as a way to write unnamed inline statement blocks that can be executed in a delegate invocation. C# 3.0 introduced lambda expressions, which are similar in concept to anonymous methods but more expressive and concise. These two features are known collectively as anonymous functions. In general, applications that target version 3.5 and later of the .NET Framework should use lambda expressions.
The following example demonstrates the evolution of delegate creation from C# 1.0 to C# 3.0:

class Test
{
    delegate void TestDelegate(string s);
    static void M(string s)
    {
        Console.WriteLine(s);
    }

    static void Main(string[] args)
    {
        // Original delegate syntax required 
        // initialization with a named method.
        TestDelegate testDelA = new TestDelegate(M);

        // C# 2.0: A delegate can be initialized with
        // inline code, called an "anonymous method." This
        // method takes a string as an input parameter.
        TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };

        // C# 3.0. A delegate can be initialized with
        // a lambda expression. The lambda also takes a string
        // as an input parameter (x). The type of x is inferred by the compiler.
        TestDelegate testDelC = (x) => { Console.WriteLine(x); };

        // Invoke the delegates.
        testDelA("Hello. My name is M and I write lines.");
        testDelB("That's nothing. I'm anonymous and ");
        testDelC("I'm a famous author.");

        // Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
    Hello. My name is M and I write lines.
    That's nothing. I'm anonymous and
    I'm a famous author.
    Press any key to exit.
 */

Monday, January 23, 2012

Structure

A Struct is a value type typically used to encapsulate small group of related variables.
A structure can have fields, properties, constants, operators, events, methods, constructors, indexers, nested types etc.
A struct can't have parameter less constructor, because CLR initializes all types of struct to their default values by own, so having explicit parameter less constructor will be overhead on performance, and CLR also not guarantees that the constructor will be called.         
Ex.
public struct Book
{
public string author;
public string name;
public string publisher;
public decimal price;
}
Structure Vs Class
A struct is like a class but having some differences -
A. Class is reference type and struct is value type.
B. Class created on heap while struct created on stack.
C. Class can be inherited but struct can't be inherited, A struct can't be a base of any class or struct, because they are sealed by default.
D. Using struct is recommended if having small group of members, because any where we
pass struct a copy of struct passes, so it will be performance overhead if having big size. While class with big group of members is efficient because it's reference passes.
public struct strEx
{
public int x;
}
public class clsEx
{
public int x;
}
public class clsMain
{
public static void main()
{
strEx ObjStr = new strEx();
clsEx ObjCls = new clsEx();

ObjStr.x = 225;
ObjCls.x = 225;

AssignStr(ObjStr); //A copy of object is passing to method
AssignCls(ObjCls); //Refrence of object is passing to method

Console.WriteLine("Value of x of struct is :{0}", ObjStr.x);
Console.WriteLine("Value of x of class is :{0}", ObjCls.x);
}

public static void AssignCls(clsEx cls)
{
cls.x = 500;
}
public static void AssignStr(strEx str)
{
str.x = 500;
}
}
Output -
Value of x of struct is  225
Value of x of class is 500

Sunday, January 22, 2012

Indexers

Indexers allows client code to access a collection item of a class (or interface or structure) by
using object of class as an array.
Syntax -
public int this[int x]  
{
       //  get and set accessors
}

Terminology -
A. Indexers defined by using this keyword, denotes current instance.
B. Indexer must have a parameter of any data type, else compiler will through the error.
C. Object of class behave like an array for indexer.
D. Indexer can't be static it always be the instance member.
E. Indexer can be overloaded, you can have multiple indexers with different signatures.
F. Provides syntactical benefit.
G. Indexers can be used with structures same as used with classes.
H. Indexers declared in an interface enforces to implement same signature(parameter and return type, not access modifier) indexers in Classes those implement that interface.
I. Syntax of indexer in an interface, its not have access modifier because they are public by default and do not have body of accessors because interface only declares members not defines.
int this[int index]
    {
        get;
        set;
    }


Example -
class Indexers
{
        public Indexers()
        { }

//Temprature taken at different time
float[] Temp = new float[]{56.2F, 56.7F, 56.5F, 56.9F, 58.8F,
61.3F, 65.9F, 62.1F, 59.2F, 57.5F};
public float this[int index]
{
get
{
        return Temp[index];
}
set
{
        Temp[index] = value;
}
}

//Weekdays
string[] days = new string[] { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
private string Getday(string gday)
{
    foreach (string tDay in days)
    {
        if (tDay == gday) return tDay;
    }
    return "Invalid day";
}
public string this[string day] //Overloading of indexers
{
    get
    {
        return Getday(day);
    }
}
}
public class CallIndexers
{
    public static void Main()
    {
        Indexers ObjIndexers = new Indexers();
        Console.WriteLine(ObjIndexers["Wed"]); //Output : Wed

        Console.WriteLine(ObjIndexers[2]); //Output : 56.5F
        ObjIndexers[5] = 60.02F;
        Console.WriteLine(ObjIndexers[5]); //Output : 60.02F
    }
}

Tuesday, January 10, 2012

Boxing and Unboxing

Boxing and Unboxing is act like a bridge between value types and reference type.


Boxing - Storing value type to System.Object type or any interface type that implimented by this value type is called Boxing.
Boxing is implicit, Explicit Boxing also possible but its never required.
Compiler copies value of value type variable to object type and stores it in managed heap by GC.

Unboxing - Copying object type value or an interface type value that this value type implements to a value type variable is called UnBoxing. Unboxing is explicit.

Unboxing consists two steps -
I Checking that object containing value is boxed value of given variable type.
II Copying Object containg value to value type variable.



int i = 123;
object o = i; //Boxing
int j = (int)o; //Unboxing


CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...