Generic Delegate C# with Example



Generic Delegate C# with Example

 	using System;

	namespace CSharpGenerics
	{	
		delegate T GenericDelegate(T v);
		class UsingGenericDelegate 
		{
			static int Sum(int v) 
			{
				int result = 0;
				for(int i=v; i>0; i--)
					result += i;
				return result;
			}

			static string ReverseString(string str) 
			{
				string result = "";
				foreach(char ch in str)
					result = ch + result;
				return result;
			}

			static void Main( )
			{
			// Construct an int delegate.
			GenericDelegate intDelegate = Sum;
			int value = 5;
			Console.WriteLine("Total of 1 to {0} : {1}", value, intDelegate(value));

			// Construct a string delegate.
			GenericDelegate strDelegate = ReverseString;
			Console.WriteLine(strDelegate("This is Reversed String."));
			}
		}
	}
 

0 Comment's

Comment Form

Submit Comment