1
Reply

Assigning object reference to interface variable why?

Ask a question
Rajeev Punhani

Rajeev Punhani

8 years ago
269
1
Hi All,
 
Could anyone please provide any link or explaination of why we create interface reference variable and assign the object of the implemented class to that(Line No.17).Here is the sample i got from msdn.
  1. interface ISampleInterface  
  2. {  
  3.     void SampleMethod();  
  4. }  
  5.   
  6. class ImplementationClass : ISampleInterface  
  7. {  
  8.     // Explicit interface member implementation:   
  9.     void ISampleInterface.SampleMethod()  
  10.     {  
  11.         // Method implementation.  
  12.     }  
  13.   
  14.     static void Main()  
  15.     {  
  16.         // Declare an interface instance.  
  17.         ISampleInterface obj = new ImplementationClass();  
  18.   
  19.         // Call the member.  
  20.         obj.SampleMethod();  
  21.     }  
  22. }  
Thanks
Regards
Rajeev 
 
 

Answers (1)