Generic Extension Method C# with Example



Generic Extension Method C# with Example

 	using System;

	namespace CSharpGenerics
	{	
		static class ExtensionMethod
		{
			public static void Print(this GenericMethodClass h)
			{
				T[] vals = h.GetValues();
				Console.WriteLine("{0},\t{1},\t{2}", vals[0], vals[1], vals[2]);
			}
		}

		class GenericMethodClass
		{
			T[] Vals = new T[3];
			public GenericMethodClass(T v0, T v1, T v2)
			{ 
				Vals[0] = v0; 
				Vals[1] = v1; 
				Vals[2] = v2; 
			}
			
			public T[] GetValues() 
			{ 
				return Vals; 
			}
		}

		class Program
		{
			static void Main(string[] args) 
			{
				var intGeneric = new GenericMethodClass(30, 15, 57);
				var stringGeneric = new GenericMethodClass("Kuldeep", "Mukesh", "Rahul");
				intGeneric.Print();
				stringGeneric.Print();
			}
		}
	}
 

0 Comment's

Comment Form

Submit Comment