More Services

Seo Services

Featured Posts

[Travel][feat1]

FOLLOW ON FACEBOOK

Seo Services

oops part three

June 18, 2018
pat thre.................
oops part three oops part three Reviewed by programming with shri on June 18, 2018 Rating: 5

part two

June 18, 2018
part two
part two part two Reviewed by programming with shri on June 18, 2018 Rating: 5

Csharp Tutorials

June 18, 2018
oops part one
Csharp Tutorials Csharp Tutorials Reviewed by programming with shri on June 18, 2018 Rating: 5

Test page 3

In this post, we will discuss the types of Interface in C#.NET with an example in a simple way.
We have already discussed the basic concept of interface in part one of this series. If you want to learn please click on the link basic interface concept.
Type of interface in C#
  • Explicit Interface
  • Implicit Interface 
  1. Explicit Interface:
    If the class implement two interfaces that contain a member with the same signature.to implementing those interface member in class we take help of explicit interface.
How to implement the explicit interface?
  1. return_Type Interface_Name.Method_Name()  
  2.         {  
  3.             //...  
  4.         }  
Note: The modifier is not valid to implement explicit interface. 
Example 1:
Create two interfaces having only one method with the same signature as follow. 
  1. interface IVendor  
  2.    {  
  3.        void GetVendor();  
  4.    }  
  5.    interface ISupplier  
  6.    {  
  7.        void GetVendor();  
  8.    }  
Create one class name like 'UserService' to implementing those interfaces. 
  1. public class UserService:IVendor,ISupplier  
  2.    {  
  3.           
  4.         void IVendor.GetVendor()  
  5.        {  
  6.            Console.WriteLine("GetVendor() method called from IVendor interface");  
  7.        }  
  8.         void ISupplier.GetVendor()  
  9.        {  
  10.            Console.WriteLine("GetVendor() method called from ISupplier interface");  
  11.        }  
  12.    }  
 If you try to implement the interface member without 'interface name', then it will through an error as following.
 
How to call the interface member?
    In explicit interface we can not call interface member directly using object of class. we have two way of calling explicit interface as following.
There are two way of calling explicit interface member: 
  1. The first way to creating the instance of the class and typecast the interface type.
  2. Second Way creating an object of the class and reference variable of the interface type.
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            // The first way to creating the instance of the class and typecast the interface type.   
  6.            UserService userService = new UserService();  
  7.            ((IVendor)userService).GetVendor();  
  8.            ((ISupplier)userService).GetVendor();  
  9.   
  10.            // Second Way creating an object of the class and reference variable of the interface type.  
  11.   
  12.            IVendor vendor = new UserService();  
  13.            vendor.GetVendor();  
  14.            ISupplier supplier = new UserService();  
  15.            supplier.GetVendor();  
  16.   
  17.            Console.ReadLine();  
  18.        }  
  19.    }  
 Output: 
 
You can use either one of the way of calling explicit interface member as per the requirement.
Example 2:

  1. using System;  
  2.   
  3. namespace CSInterfaceDemo.Customer  
  4. {  
  5.     interface IService1  
  6.     {  
  7.         void Print();  
  8.     }  
  9.     interface IService2  
  10.     {  
  11.         void Print();  
  12.     }  
  13.     public class Customer:IService1, IService2  
  14.     {  
  15.         void IService1.Print()  
  16.         {  
  17.             Console.WriteLine("IService1.Print()");  
  18.         }  
  19.         void IService2.Print()  
  20.         {  
  21.             Console.WriteLine("IService2.Print()");  
  22.         }  
  23.     }  
  24.   
  25.     class program  
  26.     {  
  27.         static void Main(string[] args)  
  28.         {  
  29.             // The first way to creating the instance of the class and typecast the interface type.     
  30.             Customer customer = new Customer();  
  31.             ((IService1)customer).Print();  
  32.             ((IService2)customer).Print();  
  33.   
  34.             // Second Way creating an object of the class and reference variable of the interface type.   
  35.             IService1 service1 = new Customer();  
  36.             service1.Print();  
  37.             IService2 service2 = new Customer();  
  38.             service2.Print();  
  39.   
  40.             Console.ReadLine();  
  41.         }  
  42.     }  
  43. }  
Test page 3 Test page 3 Reviewed by programming with shri on May 04, 2018 Rating: 5

test page 2

In this post, we will discuss the types of Interface in C#.NET with an example in a simple way.
We have already discussed the basic concept of interface in part one of this series. If you want to learn please click on the link basic interface concept.
Type of interface in C#
  • Explicit Interface
  • Implicit Interface 
  1. Explicit Interface:
    If the class implement two interfaces that contain a member with the same signature.to implementing those interface member in class we take help of explicit interface.
How to implement the explicit interface?
  1. return_Type Interface_Name.Method_Name()  
  2.         {  
  3.             //...  
  4.         }  
Note: The modifier is not valid to implement explicit interface. 
Example 1:
Create two interfaces having only one method with the same signature as follow. 
  1. interface IVendor  
  2.    {  
  3.        void GetVendor();  
  4.    }  
  5.    interface ISupplier  
  6.    {  
  7.        void GetVendor();  
  8.    }  
Create one class name like 'UserService' to implementing those interfaces. 
  1. public class UserService:IVendor,ISupplier  
  2.    {  
  3.           
  4.         void IVendor.GetVendor()  
  5.        {  
  6.            Console.WriteLine("GetVendor() method called from IVendor interface");  
  7.        }  
  8.         void ISupplier.GetVendor()  
  9.        {  
  10.            Console.WriteLine("GetVendor() method called from ISupplier interface");  
  11.        }  
  12.    }  
 If you try to implement the interface member without 'interface name', then it will through an error as following.
 
How to call the interface member?
    In explicit interface we can not call interface member directly using object of class. we have two way of calling explicit interface as following.
There are two way of calling explicit interface member: 
  1. The first way to creating the instance of the class and typecast the interface type.
  2. Second Way creating an object of the class and reference variable of the interface type.
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            // The first way to creating the instance of the class and typecast the interface type.   
  6.            UserService userService = new UserService();  
  7.            ((IVendor)userService).GetVendor();  
  8.            ((ISupplier)userService).GetVendor();  
  9.   
  10.            // Second Way creating an object of the class and reference variable of the interface type.  
  11.   
  12.            IVendor vendor = new UserService();  
  13.            vendor.GetVendor();  
  14.            ISupplier supplier = new UserService();  
  15.            supplier.GetVendor();  
  16.   
  17.            Console.ReadLine();  
  18.        }  
  19.    }  
 Output: 
 
You can use either one of the way of calling explicit interface member as per the requirement.
Example 2:

  1. using System;  
  2.   
  3. namespace CSInterfaceDemo.Customer  
  4. {  
  5.     interface IService1  
  6.     {  
  7.         void Print();  
  8.     }  
  9.     interface IService2  
  10.     {  
  11.         void Print();  
  12.     }  
  13.     public class Customer:IService1, IService2  
  14.     {  
  15.         void IService1.Print()  
  16.         {  
  17.             Console.WriteLine("IService1.Print()");  
  18.         }  
  19.         void IService2.Print()  
  20.         {  
  21.             Console.WriteLine("IService2.Print()");  
  22.         }  
  23.     }  
  24.   
  25.     class program  
  26.     {  
  27.         static void Main(string[] args)  
  28.         {  
  29.             // The first way to creating the instance of the class and typecast the interface type.     
  30.             Customer customer = new Customer();  
  31.             ((IService1)customer).Print();  
  32.             ((IService2)customer).Print();  
  33.   
  34.             // Second Way creating an object of the class and reference variable of the interface type.   
  35.             IService1 service1 = new Customer();  
  36.             service1.Print();  
  37.             IService2 service2 = new Customer();  
  38.             service2.Print();  
  39.   
  40.             Console.ReadLine();  
  41.         }  
  42.     }  
  43. }  
test page 2 test page 2 Reviewed by programming with shri on May 04, 2018 Rating: 5
ads 728x90 B
Powered by Blogger.