Created
February 12, 2017 18:08
-
-
Save rahulsahay19/b3ebea717ff842a5a1458c23eb2c9f91 to your computer and use it in GitHub Desktop.
generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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