Generc Method C# with Example



Generc Method C# with Example

 	using System;

	namespace CSharpGenerics
	{
		class UsingGenericMethod
		{
			// Swap two General Types.
			static void Swap(ref TYPE a, ref TYPE b)
			{
				TYPE temp;
				temp = a;
				a = b;
				b = temp;
			} 

			public static void Main()
			{
				// Swap 2 ints.
				int a = 10, b = 90;
				Console.WriteLine("Before swap integers: {0}, {1}", a, b);
				Swap(ref a, ref b);
				Console.WriteLine("After swap integers: {0}, {1}\n", a, b);
				
				// Swap 2 strings.
				string s1 = "Hello", s2 = "There";
				Console.WriteLine("Before swap strings: {0} {1}!", s1, s2);
				Swap(ref s1, ref s2);
				Console.WriteLine("After swap strings: {0} {1}!", s1, s2);
			}
		}
	}
 

0 Comment's

Comment Form

Submit Comment