Saturday, 3 September 2016

Abstract Vs Virtual method explained

There are several keywords that can be used for inheritance in object-oriented programming languages. In C#, you can specify a method as abstract or virtual.

In the concrete class, you must override any method marked as abstract. You can override 
virtual
methods. In addition, you can provide new implementation for normal methods that are neither 
abstract nor virtual. In this case, it's recommended to use the new keyword.

What's the difference between abstract and virtual methods? How can I use them to serve my design? In this article, I'm trying to answer these questions.

Of course the abstract method doesn't have implementation. It just defines the method. But is there any other differences? Let's take an example of two classes. An abstract class and another one that inherits from it.
Class diagram of an abstract class and a concrete one


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// the abstract class
public abstract class BaseClass
{
    public abstract string GetAbstractValue();
 
    public virtual string GetVirtualValue()
    {
        return "Base GetVirtualValue";
    }
 
    public string GetValue()
    {
        return "Base GetValue";
    }
}
 
 
// the concrete class
public class ConcreteClass : BaseClass
{
    public override string GetAbstractValue()
    {
        return "Concrete GetAbstractValue";
    }
 
    public override string GetVirtualValue()
    {
        return "Concrete GetVirtualValue";
    }
 
    public string GetValue()
    {
        return "Concrete GetValue";
    }
}

Let's make a console application that uses the above classes. We want to make 2 instances of the concrete class. The first one should be of type 
BaseClass and the other one of type ConcreteClass.

The client code should look like this:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Program
{
    static void Main(string[] args)
    {
        BaseClass instance1 = new ConcreteClass();
             
        Console.WriteLine(instance1.GetAbstractValue());
        Console.WriteLine(instance1.GetVirtualValue());
        Console.WriteLine(instance1.GetValue());
 
 
        ConcreteClass instance2 = new ConcreteClass();
 
        Console.WriteLine(instance2.GetAbstractValue());
        Console.WriteLine(instance2.GetVirtualValue());
        Console.WriteLine(instance2.GetValue());
 
 
        Console.Read();
    }
}

Let's now see and compare the results.

The results of running the application

What do you conclude from the results?

Let's make another experiment before conclusion. Let's remove the following 2 methods from the 
ConcreteClass

  • GetVirtualValue
  • GetValue

Try to run the application and notice the results.

The results after removing the 2 methods

From the previous results we can conclude the following:


AbstractVirtualNo Keyword
Can have implementation? NoYesYes
Can override?MustCan but not a mustYou can declare a new method with the same name
Which keyword to use to provide new implementation in the concrete class?overrideoverrideNo keyword needed
If an object is created of the base class type, which method will be executed?Concrete implementationThe parent implementation will be called only if no implementation is provided in the concrete classParent implementation
If an object is created of the concrete class type, which method will be executed?Concrete implementationConcrete implementationThe parent implementation will be called only if no implementation is provided in the concrete class

Invoke and BeginInvoke and EndInvoke Pattern

In this article, we will explore multiple ways to call a method asynchronously using Delegates. You can call methods asynchronously in four different ways using the BeginInvoke() and EndInvoke() methods of the Delegate class. The four different ways are using the EndInvoke pattern, WaitHandle, Polling pattern and using a Callback Pattern. In this article, we will see the Delegate EndInvoke Pattern. In the subsequent articles, we will explore the Polling Pattern and Callback pattern.
A delegate is a special method that encapsulates a method and represents a method signature. From the main thread, when a method is invoked using a delegate, the Main thread has to wait until the method, that was invoked, has completed. This could prove expensive if your code is performing a slow operation like downloading a large file from your server.
Let us see an example of how a method is called synchronously using a Delegate:
Synchronous Method Delegate
As you can see, the Main method has to wait until the method SquareMethod completes. This is because SquareMethod was executed on the same thread, as that of the Main method.
However with a little effort, you can use delegates to call any method asynchronously (works when delegate has only one method in its invocation list). This is done using the BeginInvoke and EndInvoke methods of the Delegate class.
BeginInvoke, EndInvoke and IAsyncResult
The BeginInvoke method initiates the asynchronous call on a separate thread taken from the ThreadPool. It consists the same parameters required by the method (that you want to execute asynchronously) and consists two additional optional parameters called the callback parameter and the state parameter. The advantage of using BeginInvoke is that it returns immediately and does not wait for the asynchronous call to complete.
BeginInvoke returns a reference to an object implementing the IAsyncResult interface, which can be used to monitor the progress of the asynchronous call.
The EndInvoke method retrieves the results of the asynchronous call and releases the resource used by the thread. The parameters of EndInvoke include the out and ref parameters of the method that you want to execute asynchronously, plus a reference to the IAsyncResult returned by the BeginInvoke.
If the BeginInvoke() and EndInvoke() are not clear yet, worry not, as we will see an example shortly.
Call a Method Asynchronously using Delegate
Now that you know how the BeginInvoke and EndInvoke methods of the Delegate class work, let us see the different ways/patterns available to use BeginInvoke() and EndInvoke() to make asynchronous calls
EndInvoke Pattern - In this pattern, we use BeginInvoke from the Main thread to call the method, then do some processing on the main thread and then call EndInvoke(). This is useful when you want to have the calling thread to continue processing at the same time the asynchronous call is executing. Here the EndInvoke() does not return until the asynchronous call has completed. In other words, the main thread waits till the asynchronous operation is complete.
WaitHandle – Using this technique, the main method calls the method asynchronously and waits for a WaitHandle before it calls EndInvoke().
Polling Pattern - In this pattern, the calling thread polls the other thread (doing async operation) periodically using the IAsyncResult object and checks whether the thread has completed. If not, it continues processing and checks the thread later. The application does not call EndInvoke() until it knows that the operation is complete. This pattern can be useful when you want your UI to be responsive, till the async operation completes.
Callback Pattern - In this pattern, the initial thread initiates the async call but does not wait or check to see if the thread that was called, has completed. This pattern can be useful if you not want to process the results of the async call in the main thread.
Let us see an example of calling a method asynchronously using the EndInvoke Pattern
Asynchronous Method Delegate
As you can see, BeginInvoke() is used to initiate the asynchronous call, which returns immediately after the call is made, so the Main method can continue processing while the async operation executes. The BeginInvoke() returns an IAsyncResult object to the calling thread.
IAsyncResult asyncRes = sd.BeginInvoke(10, null, null);
We then use the EndInvoke() function to retrieve the results of the asynchronous call. The EndInvoke() has a parameter that is a reference to the IAsyncResult, returned by the BeginInvoke.
int res = sd.EndInvoke(asyncRes);
EndInvoke() uses this parameter to find the thread it refers to, which can be used to monitor the progress of the asynchronous call.
That’s it. We just made an asynchronous call to a method using a Delegate!
In the next article, I will show you how to use the Polling pattern and Callback pattern to perform async calls on methods.


using System;
using System.Threading;

namespace DelegateAsyncMethods
{
    delegate int SomeDelegate(int x);

    /// <summary>
    /// This code is written for the article 
    /// www.dotnetcurry.com/ShowArticle.aspx?ID=634
    /// </summary>
    class Program
    {
        static void Main(string[] args) {
            SomeDelegate sd = SquareNumber; // Create delegate instance
            Console.WriteLine("Before SquareNumber Method Invoke");
            IAsyncResult asyncRes = sd.BeginInvoke(10, null, null);            
            Console.WriteLine("Back to Main Method");
            int res = sd.EndInvoke(asyncRes);
            Console.WriteLine(res);
            Console.ReadLine();
        }

        static int SquareNumber(int a) {
            Console.WriteLine("SquareNumber Invoked. Processing..");
            Thread.Sleep(2000);
            return a * a;
        }
    }

}

Is exception in one delegate will Stop execution of rest of the deligate?

Yes It stops execution of all rest delegates

How to handle it

Here is Problem :

public delegate void TheMulticastDelegate(int x,int y);
    class Program
    {
            private static void MultiCastDelMethod(int x, int y)
            {
                    try
                    {
                            int zero = 0;
                            int z = (x / y) / zero; 
                    }
                    catch (Exception ex)
                    {                               
                            throw ex;
                    }
            }

            private static void MultiCastDelMethod2(int x, int y)
            {
                    try
                    {
                            int z = x / y;
                            Console.WriteLine(z);
                    }
                    catch (Exception ex)
                    {
                            throw ex;
                    }
            }
            public static void Main(string[] args)
            {
                    TheMulticastDelegate multiCastDelegate = new TheMulticastDelegate(MultiCastDelMethod);
                    TheMulticastDelegate multiCastDelegate2 = new TheMulticastDelegate(MultiCastDelMethod2);

                    try
                    {
                            TheMulticastDelegate addition = multiCastDelegate + multiCastDelegate2;

                            foreach (TheMulticastDelegate multiCastDel in addition.GetInvocationList())
                            {
                                    multiCastDel(20, 30);
                            }
                    }
                    catch (Exception ex)
                    {
                            Console.WriteLine(ex.Message);
                    }

                    Console.ReadLine();
            }
    }


Here is Solution :

foreach (TheMulticastDelegate multiCastDel in addition.GetInvocationList())
{
    try
    {
        multiCastDel(20, 30);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}