Interface Constraint Derived Class C# with Example



Interface Constraint Derived Class C# with Example

 	using System;

	namespace CSharpGenerics
	{
		interface IDisplay 
		{
			void Display();
		}
		
		class BaseClass : IDisplay
		{
			virtual public void Display()
			{
				Console.WriteLine("IDisplay Implemented Base Class Method.");
			}

		}

		class OtherClass : BaseClass
		{
			override public void Display()
			{
				Console.WriteLine("IDisplay Implemented OtherClass Method.");
			}
		}
		
		class GenClass where T : IDisplay
		{
			T genObject;
			
			public GenClass(T argObject)
			{
				genObject = argObject;
			}
			
			public void DisplayMessage()
			{
				genObject.Display();
			}
		}
		
		class InterfaceConstraints
		{
			static void Main( )
			{
				BaseClass baseObject = new BaseClass();
				OtherClass otherObject = new OtherClass();
				
				GenClass genBaseObject = new GenClass(baseObject);
				genBaseObject.DisplayMessage();
				
				GenClass genOtherObject = new GenClass(otherObject);
				genOtherObject.DisplayMessage();
			}
		}
	}
 

0 Comment's

Comment Form

Submit Comment