In this topic i will explain about Generics with example. am going to compare two variable to explain about generics.
find the code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Generics
{
// declare generics
public class MyClass<T>
{
public bool compare(T a, T b)
{
if (a.Equals(b))
{
return true;
}
else
{
return false;
}
}
}
class Program
{
static void Main(string[] args)
{
MyClass<string> check = new MyClass<string>();
bool res = check.compare("A","A");
Console.Write(res);
MyClass<int> check1 = new MyClass<int>();
bool res1 = check1.compare(5, 10);
Console.Write(res1);
Console.ReadKey();
}
}
}
find the code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Generics
{
// declare generics
public class MyClass<T>
{
public bool compare(T a, T b)
{
if (a.Equals(b))
{
return true;
}
else
{
return false;
}
}
}
class Program
{
static void Main(string[] args)
{
MyClass<string> check = new MyClass<string>();
bool res = check.compare("A","A");
Console.Write(res);
MyClass<int> check1 = new MyClass<int>();
bool res1 = check1.compare(5, 10);
Console.Write(res1);
Console.ReadKey();
}
}
}
Nanri bro , sample code really helpful
ReplyDelete