Saturday, July 23, 2011

C# Notes

C# important notes:

1.  Delegates provide a way of giving a level of indirection, so that instead of specifying behavior directly, it can be in some way "contained" in an object, which can be used like any other object, where one available option is to execute the encapsulated behavior.

A delegate type is a single-method interface , and a delegate instance is an object implementing that interface.

Delegates are typically used when the code that wants to execute the actions doesn't know the details of what the action should be.

Four things need to happen:

■ The delegate type needs to be declared.
■ There must be a method containing the code to execute.
■ A delegate instance must be created.
■ The delegate instance must be invoked.

Delegate Type :
A delegate type is effectively just a list of parameter types and a return type. It specifies what kind of action can be represented by instances of the type. For instance, consider a delegate type declared like this:

delegate void StringProcessor (string input);

The code says that if we want to create an instance of StringProcessor, we’re going to need a method with one parameter (a string) and a void return type (the method doesn’t return anything). It’s important to understand that StringProcessor really is a type. It has methods, you can create instances of it, pass around references to instances, the whole works.

In .NET, delegate instances always refer to methods.

void PrintInteger (int x)

An instance of that delegate type, specifying that this method be executed when the delegate instance is invoked. Action of the delegate instance.

StringProcessor proc1, proc2;
proc1 = new StringProcessor(StaticMethods.PrintString);

InstanceMethods instance = new InstanceMethods();
proc2 = new StringProcessor(instance.PrintString);

Example 1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegates_28July2011_7PM
{

    //1. Declare delegate type.
    delegate void StringProcessor(string input);

    class Person
    {
        public string Name { get; private set; }


        public Person(string name)
        {
            Name = name;
        }

        //2. Declare compatible INSTANCE method.
        public void Say(string message)
        {
            Console.WriteLine("{0} says: {1}", Name, message);
        }
    }

    class Background
    {
        //2. Declare compatible STATIC method.
        public static void Not(String Note)
        {
            Console.WriteLine("{0}", Note);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person objPerson1 = new Person("Ram");

            Person objPerson2 = new Person("Shyam");

            //3. Creates three delegate instance
            StringProcessor ramVoice, shyamVoice, background;
            ramVoice = new StringProcessor(objPerson1.Say);
            shyamVoice = new StringProcessor(objPerson2.Say);
            background = new StringProcessor(Background.Not);

            //4. Invokes delegates instance.
            ramVoice("Namaskar");
            shyamVoice("Skhaa");
            background("This is really sweet");

            Console.ReadLine();

        }
    }
}

No comments: