Sunday, February 9, 2014

Anonymous methods in c#



Basically anonymous methods are methods without a name, but only the body. These methods are useful when you want to declare a method that is doing simple work. It came with C# 2.0 as a solution to declare a _delegate_ without the need of a separate named method. The syntax for such a declaration is as follows.
public class Program
    {
        public delegate void DelegateType();

        static void Main(string[] args)
        {
            DelegateType DelegateObject = delegate()
            {
                Console.WriteLine("AnonymousMethod is invoked");
            };

            DelegateObject();
            Console.ReadKey();
        }
    }



Note that you can write anonymous methods of any return type and parameters.
public class Program
    {
        public delegate int DelegateType(int x);

        static void Main(string[] args)
        {
            DelegateType DelegateObject = delegate(int x)
            {
                return x;
            };

            int result = DelegateObject(5);

            Console.WriteLine("Result is : {0}", result);
            Console.ReadKey();
        }
    }



Though we define an anonymous method one time we can reuse it because we are wrapping it in a delegate.

Anonymous methods can be useful in a situation when we find a named method is an unnecessary overhead. A good example would be when you start a new thread. Look at the following example.
public class Program
    {
        public delegate void DelegateType();

        static void Main(string[] args)
        {
            System.Threading.Thread thread = new System.Threading.Thread
              (delegate()
              {
                  System.Console.Write("This Thread is performing work");
              });
            thread.Start();
            Console.ReadKey();
        }
    }


Saturday, February 8, 2014

_delegate_ in c#

_delegate_ is a reference type in c# which can be used to create a new delegate type. This new delegate type also acts as a reference type that can store a reference to an instance of a delegate Object. A delegate Object is a special kind of object which can hold references to certain methods and later be used to invoke those methods via the delegate Object. Delegates seem to work like Function pointers in C or C++, but they are totally different to Function pointers because they are object-oriented, type-safe, and secure.

Now we can see that there are three basic steps in using a delegate.

1.  Create a new delegate type using delegate keyword. => Declaring a delegate

2. Create a delegate object from new delegate type and populate the object with references to methods. => Instantiating a delegate

3. Use delegate object to invoke the methods encapsulate by the object. => Calling a delegate

Let's discuss these three steps in detail.

1. Declaring a delegate

Declare the delegate in a way that it resembles the signature of the method or methods you will later  associate with it. Following is the structure of declaring a delegate type.

delegate return_type new_type_name(argument list)

   Examples :-

   delegate void delageteType();

   delegate string delageteTypeA(int a, string b);


   delegate TestClass delageteTypeB(double a);


Also you can add access modifiers with delegates as any other class member.

   Ex:- public delegate void delageteType();

2.  Instantiating a delegate

This step includes two sub-steps namely,
   I. Create a delegate object of the newly declared delegate type while associating it with a particular method.
  II. This is optional. Associate the object with methods of same signature (i.e. multicast).

Assume we have functions as follows.

class Program{
   static void testFunctionA()
        {
            Console.WriteLine("functionTestA is invoked");
        }

   static void testFunctionB()

        {
            Console.WriteLine("functionTestB is invoked");
        }

   private void testFunctionC()

        {
            Console.WriteLine("functionTestC is invoked");
        }
}

Now we can instantiate our delegate following the aforementioned sub-steps.

Example for sub-step I,

   DelegateType DelegateObject = new DelegateType(testFunctionA);

Alternatively you can use the following syntax to do the above.
   
   DelegateType DelegateObject = testFunctionA;

Example for sub-step II,

   DelegateObject +=  testFunctionB;

Note that we can add any type of method with same signature to the delegate object. For example we can add an instance method with the same signature as follows.

  DelegateObject +=  new Program().testFunctionC;

Note that as we can add methods using += we can also remove existing methods from delegate object using -=.

   DelegateObject -=  testFunctionB;

3. Calling a delegate

Calling the delegate object is like calling a method.  It is called by the name of the delegate object, followed by parenthesized arguments.

Examples :-

   DelegateObject(); // delegate type declaration => delegate void delageteType();

   string result = DelegateObjectA(2, "test"); // delegate type declaration => delegate string delageteTypeA(int a, string b);

   TestClass resultObject = DelegateObjectB(1.204); // delegate type declaration => delegate TestClass delageteTypeB(double a);

Now let's sum up all the above code in a simple c# class called Program.cs.

class Program
    {
        public delegate void DelegateType(); //Step 1

        static void testFunctionA()
        {
            Console.WriteLine("functionTestA is invoked");
        }

        static void testFunctionB()
        {
            Console.WriteLine("functionTestB is invoked");
        }

        private void testFunctionC()
        {
            Console.WriteLine("functionTestC is invoked");
        }

        static void Main(string[] args)
        {
            DelegateType DelegateObject = testFunctionA; // Step 2 -I
            DelegateObject += testFunctionB; // Step 2 -II
            DelegateObject += new Program().testFunctionC; // Step 2 -II
            DelegateObject -= testFunctionB; // Step 2 -II

            DelegateObject(); // Step 3
            Console.ReadKey();
        }
    }








Sunday, October 27, 2013

_namespace_ in c#

In c# we use namespace keyword to declare a logical scope. You can create various programming elements (i.e. class, method, etc. ) inside a particular namespace and these elements will be unique within that namespace. 

You can refer to these elements globally from anywhere inside your project or solution by referring to the namespace they belong. This implies that the namespace provide a way to use elements in a globally unique manner.

Now let's see what type of elements (i.e. types) you can create within a namespace.

1. class
2. interface
3. struct
4. enum
5. delegate
6. another namespace

namespace TestNamespace
{
    class TestClass { }
    interface TestInterface { }
    struct TestStruct { }
    enum TestEnum { }
    delegate void TestDelegate();
    namespace TestNamespace.NestedNamespace { }
}


Also In the same project you can have another namespace with the same types as follows.

namespace TestNamespace
{
    class TestClass { }
    interface TestInterface { }
    struct TestStruct { }
    enum TestEnum { }
    delegate void TestDelegate();
    namespace TestNamespace.NestedNamespace { }
}


Also note that we can refer any of these types uniquely as follows.

namespace TestNamespaceA
{
    public class TestClassA
    {
        TestClassA()
        {
            new TestClassA(); /* line1 */
            new TestNamespaceA.TestClassA(); /* line2 */
            new TestNamespaceB.TestClassB(); /* line3 */
        }
    }
}

namespace TestNamespaceB
{
    class TestClassB
    {

    }

}

Here line1 and line2 serve the same end.

Further we can omit the namespace and create types. Then these types will fall into an unnamed namespace sometimes known as default or global namespace. This is known as global namespace because the types in this namespace can be accessed from any namespace in the project.

namespace TestNamespaceA
{
    public class TestClassA
    {
        TestClass()
        {
            new ClassInGlobalNamespace(); /* line1 */
            new TestClassB(); /* line2 */
            new TestNamespaceB.TestClassB(); /* line3 */
        }
    }
}

namespace TestNamespaceB
{
    class TestClassB
    {

    }
}

class ClassInGlobalNamespace
{

}

Note that line1 and line3 will compile but not line2. This implies that a type in ClassInGlobalNamespace can be accessed from any named namespace without specifying a reference to the ClassInGlobalNamespace  namespace. Actually we don't have a so called reference to global namespace anyway.

Two other important things are,
  • Namespaces implicitly have public access and this is not modifiable in fact you can't put any modifiers or attributes to a namespace declaration.
  •  It is possible to define a namespace in multiple declarations as follows.

namespace TestNamespace
{
    public class TestClassA { }
}

namespace TestNamespace
{
    public class TestClassB { }
}