Generic Method:
C# also provides Generic Methods. We can create a method which defer the parameter data type until the method is called. These parameters are called Type parameters that means we can pass the actual data type later.
In that method we have declared a Type argument after the method name <T>. T is the type argument.
Code:
C# also provides Generic Methods. We can create a method which defer the parameter data type until the method is called. These parameters are called Type parameters that means we can pass the actual data type later.
In that method we have declared a Type argument after the method name <T>. T is the type argument.
Code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace genericMethod
- {
- class Program
- {
- public static void compareTo<T>(T a, T b)
- {
- if (a.Equals(b))
- Console.WriteLine("True");
- else
- Console.WriteLine("False");
- }
- static void Main(string[] args)
- {
- compareTo<int>(12, 12);
- compareTo<int>(12, 11);
- compareTo<float>(12.45f, 11.45f);
- compareTo<string>("Sujan","Sujan");
- Console.ReadKey();
- }
- }
- }
0 Comments:
Post a Comment