Interface Constraints C# with Example



Interface Constraints C# with Example

 	using System;

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

		}

		class OtherClass : IDisplay
		{
			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