Explicit interface implementation

CSharp · Console

/Program.cs

using System;
	
namespace Test {
	
	public interface ISomeInterface1 {
		void SomeMethod();
		int SomeProperty {get; set;}
		event EventHandler SomeEvent;
		int this[string index] {get; set;}
	}
	
	public class ExplicitImplemantion : ISomeInterface1 {
		
		private EventHandler someEventVar;
		
		void ISomeInterface1.SomeMethod(){
			//this method belongs to teh interface only 
			// do some stuff here
			Console.WriteLine("This is method belongs to interface");
		}
		
		public void SomeMethod(){
			// do some stuff here
			// this method belong to teh class only
			Console.WriteLine("This is method belongs to class");
		}
		
		int ISomeInterface1.SomeProperty {get; set;}
		
		event EventHandler ISomeInterface1.SomeEvent {
			add { someEventVar += value;  } 
			remove {someEventVar -= value; }
		}
		
		int ISomeInterface1.this[string index] {
			get{
				return 0;
			}
			set{
				// do some assignation to a collection basign on index passed
				// arr[index] = value;
			}
		}
	}

	
	public class Program
	{
		public static void Main()
		{
			ExplicitImplemantion obj = new ExplicitImplemantion();
			ISomeInterface1 interfaceObj = obj as ISomeInterface1;
			
			obj.SomeMethod();        // this is not allowed operation
			interfaceObj.SomeMethod(); // this is allowed operation
			
			//int val1 = obj.SomeProperty;        // this is not allowed operation
			int val2 = interfaceObj.SomeProperty; // this is allowed operation
			
			//obj.SomeEvent += someHandler;        // this is not allowed operation
			//interfaceObj.SomeEvent += EventHandler; // this is allowed operation
			
			//int val3 = obj["someKey"];        // this is not allowed operation
			int val4 = interfaceObj["someKey"]; // this is allowed operation
			
		}
	}
}
;