Base Class Constraints C# with Example



Base Class Constraints C# with Example

 	using System;

	namespace CSharpGenerics
	{
		class BaseClass 
		{
			public void Display()
			{
				Console.WriteLine("This is BaseClass Method.");
			}
		}
		
		class DerivedClass : BaseClass{}

		class OtherClass
		{
			public void Display()
			{
				Console.WriteLine("This is OtherClass Method.");
			}
		}
		
		class GenClass where T : BaseClass
		{
			T genObject;
			
			public GenClass(T argObject)
			{
				genObject = argObject;
			}
			
			public void DisplayMessage()
			{
				genObject.Display();
			}
		}
		
		class BaseClassConstraints
		{
			static void Main( )
			{
				BaseClass baseObject = new BaseClass();
				DerivedClass derivedObject = new DerivedClass();
				OtherClass otherObject = new OtherClass();
				
				GenClass genBaseObject = new GenClass(baseObject);
				genBaseObject.DisplayMessage();
				
				GenClass genDerivedObject = new GenClass(derivedObject);
				genDerivedObject.DisplayMessage();
				
				//GenClass genOtherObject = new GenClass(otherObject);
				//genOtherObject.DisplayMessage();
			}
		}
	}
 

0 Comment's

Comment Form

Submit Comment