Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Generic Reference constraint
/* while using Reference type constraints we create objects of type class, but when we pass the same object to the generic class we cannot access the methods of the cass. off course we can have base class constraints i just have doubt on thid*/ using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class FooBase { } public class Foo:FooBase { public Foo() { } public Foo(Foo x,Foo y) { ThisX=x; ThisY=y; } public Foo ThisX{get;set;} public Foo ThisY{get;set;} public void FooMethod(Foo ob) { Console.WriteLine("Hiiiii\t"+ob); } } public class Gen<T> where T:FooBase { T obj; public Gen() { } public Gen(T x,T y) { Console.WriteLine("Gen(T x,T y)\t"+x.GetType().Name.ToString()+" "+y.GetType().Name.ToString());//+" "+obj.GetType().Name.ToString()); } public void Display(T ob) { obj=ob; Console.WriteLine("Display "+ob.GetType().Name.ToString()+" "+obj.GetType().Name.ToString()); obj.FooMethod(obj); } } public class Program { public static void Main(string[] args) { Gen<Foo> m=new Gen<Foo>(); Foo ob1=new Foo(); Foo ob2=new Foo(); Console.WriteLine("Main "+m.GetType()); Console.WriteLine("Main "+ob1.GetType()); Console.WriteLine(); Gen<Foo> n=new Gen<Foo>(ob1,ob2); Console.WriteLine(); m.Display(ob1); } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Main 4-3
Linear search
count occurances
codejam1a1
Bubble Sort
Triangulo de pascal
frgthyji8u7ytgfd
Arrange Hex string in a new one as a byte array declaration
24 101 251 Primes
Value Type vs Ref Type
Please log in to post a comment.