Tuesday 2 February 2016

Difference between Singleton and Static class?



Singleton:-
1)      Singleton is a design pattern which is a solution for recurring to the problems.
2)      Singleton is used to create the object of class only once.
3)      Singleton may contain non-static member as well.
4)      Singleton class has a constructor.
5)      Does not have the restriction for inheritance.
6)      Can dispose object of singleton class.

Static Class:-
1)      Cannot create instance of static class.
2)      Does not contain non-static member.
3)      Does not have constructor.
4)      Cannot inherit static class to another static class.
5)      Load automatically by .net framework CLR.

Monday 1 February 2016

why we use interface in wcf instead of abstract?



In dotnet intserfaces are used for describing behaviour.
WCF,Web Service and remoting this all technology uses RPC(Remote Procedure Calling) behaviour.

Abtract class not used because class not support multiple interface.
In WCF rules, If a clas has been marked with a ServiceContract attribute then another class cannot inherite from it.


Example:-
Just make a simple service for adding and subtracting nos.

        [ServiceContract]
        Public abstract class MathAbstract
        {
           [OperationContract]
           Public Abstract Int Substract(int num1,int num2);
  
           [OperationContract]
           Public Abstract Int Add(int num1,int num2);
  

        }


        Public Class Math:MathAbstract
        {
           public Override int Add(int num1, int num2)
           {
               return num1 + num2;
           }
  
           public Override int Subtract(int num1, int num2)
           {
               return num1 - num2;
           }
        }


Please run this service then you will get the following error :-
"The service class of type Service both defines a ServiceContract and inherits a ServiceContract from type IService. Contract inheritance can only be used among interface types.  If a class is marked with ServiceContractAttribute, then another service class cannot derive from it."



Note: This content has taken from different blogs or post.