Skip to content

Instantly share code, notes, and snippets.

@rahulsahay19
Created February 12, 2017 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulsahay19/b3ebea717ff842a5a1458c23eb2c9f91 to your computer and use it in GitHub Desktop.
Save rahulsahay19/b3ebea717ff842a5a1458c23eb2c9f91 to your computer and use it in GitHub Desktop.
generics
using System;
namespace Generic_Constraints
{
class MyClassOne
{
public MyClassOne()
{
Console.WriteLine("Printing Ctor 1");
}
public void MethodA()
{
Console.WriteLine("Printing Method A");
}
public void MethodB()
{
Console.WriteLine("Printing Method B");
}
}
class MyClassTwo
{
public MyClassTwo()
{
Console.WriteLine("Printing Ctor 2");
}
public MyClassTwo(int i, double d)
{
Console.WriteLine("Printing Params");
}
}
interface IInterfaceOne
{
void MethodIA();
}
interface IInterfaceTwo
{
void MethodIB();
}
class MyClassImplementingAll :MyClassOne, IInterfaceOne, IInterfaceTwo
{
public void MethodIB()
{
Console.WriteLine("Printing Interface B");
}
void IInterfaceOne.MethodIA()
{
Console.WriteLine("Printing Interface A");
}
}
class Program
{
static T GenericMethod<T>() where T:MyClassImplementingAll, IInterfaceOne, new()
{
T obj = new T();
obj.MethodA();
obj.MethodB();
obj.MethodIA();
obj.MethodIB();
return obj;
}
static void Main(string[] args)
{
GenericMethod<MyClassImplementingAll>();
// GenericMethod<MyClassTwo>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment