Tuesday, February 15, 2011

FAQ --- Part 1

Event: An event is a message sent by an object to signal the occurance of an action. The action could be caused by the user interaction such as a mouse click or it could be triggered by some other program.

1. The object that raises the event is called the event sender.
2. The object that captures the event and responds to it is called the event receiver.

In event communication, the event sender class does not know which object or method will receive (handle) the events it raises. What is needed is an intermediary (or pointer-like mechanism) between the source and the receiver. The .NET Framework defines a special type (Delegate) that provides the functionality of a function pointer.

Delegates: A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature.

There are three steps in defining and using delegates:
Declaration
Instantiation
Invocation

A very basic example (SimpleDelegate1.cs):

using System;

namespace Akadia.BasicDelegate
{
// Declaration
public delegate void SimpleDelegate();

class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}

public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

// Invocation
simpleDelegate();
}
}
}

Compile an test:

# csc SimpleDelegate1.cs
# SimpleDelegate1.exe
I was called by delegate ...



Delegates in 1.1
///
/// Declaring a Class with Sum Method
///
public class clsDelegate
{
public int Sum(int a, int b)
{
return a + b;
}
}

// Delegate Declaration
public delegate int SumDelegate(int x,int y);

private void btnSubmit_Click(object sender, EventArgs e)
{
// Delegate Initialization
SumDelegate obj = new SumDelegate(new clsDelegate().Sum);

// Invoking Delegate
MessageBox.Show("Sum: " + obj(2 ,3).ToString());
}

Delegates in 2.0
public delegate int AddDelegate(int a, int y);
private void btnSubmit1_Click(object sender, EventArgs e)
{
AddDelegate obj= delegate(int x,int y) {return x + y;};
MessageBox.Show(obj(5,7).ToString());
}


Anonymous Method
Below is an exapmle of using an anonymous method.

public delegate int ChangeInt(int x);

ChangeInt myDelegate = new ChangeInt(
delegate(int x)
{
return x * 2;
}
);


Understainding Lambda Expression:
It is a simplest form for writing an anonymous method:
ChangeInt myDelegate = x => x * 2;

In case we have more than 1 parameter:
If you have a delegate that takes two arguments:

// Defines a delegate that takes two ints and returns an int
public delegate int MultiplyInts(int arg, int arg2);

You can declare and initialize a delegate:
MultiplyInts myDelegate = (a, b) => a * b;


In case delegate return type is void,but have a parameter then Lambda expression will be:
// Defines a delegate that takes a string and returns void
public delegate void OutputToConsole(string arg);

static void Main(string[] args)
{
OutputToConsole o = a => {
Console.WriteLine(a);
};
o("Hello, World");
}


In case delegate have returns void and takes no arguments, it results in interesting syntax:
// Defines a delegate that takes no arguments and returns void
public delegate void OutputHelloToConsole();

static void Main(string[] args)
{
OutputHelloToConsole o = () =>
{
Console.WriteLine("Hello, World");
};
o();
}



Extension Method
Another new concept that comes with .NET 3.5 is the Extension methods. Now we can include our own custom methods in already defined objects. We can create static classes and include custom methods to objects. Extension method behavior is similar to that of static methods. You can declare them only in static classes. To declare an extension method, you specify the keyword this as the first parameter of the method. Let us look at the following example:
public static class ExtensionMethods
{
public static int ToInt32Extension(this string s)
{
return Int32.Parse(s);
}
}

If we include the above class namespace to our application, any string variable will have ToInt32Extension method.

This function takes 0 arguments and passes the string to s. We can also pass parameters just like below:
public static class ExtensionMethods
{
public static int ToInt32ExtensionAddInteger(this string s,int value)
{
return Int32.Parse(s) + value;
}
}

Here integer value is also passed.
string s = "10";
s.ToInt32Extension(); it'll return int data as declared above in function.


Anonymous Type
Annonymous types introduced with .NET can create object without even declaring the classes. The members of the
anonymous types are implicitely defined during compilation

var x = new { Name ="AA", Age =30, ... };
This would create a new object of annonymous type.
when we check it like x. then it'll list proerties listed above in x.


Meaning of VAR
Var is a new keyword added in .NET 3.5. This is called Implicit Type Variable. During runtime it automatically
assigns its type of the variable based on the object if finds.

var x = 10 // implies the variable is of integer type
var x = new list(); // Means x holds list of employees

var is very useful when working with annonymous typed just introduced. It assigns the type of the variable based on
the type of the object passed in, so there is no difference of doing List() x = new List() and doing var x = new List() as in both the case the type of the variable x is always get to List.

Only restriction is we cannot define members of var type.

No comments:

Post a Comment