Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Generics Base class constraints Doubts
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { class A { public object GetOb() { return this; } public int Add(int m,int n) { return(m+n); } public void Hello(object ob) { Console.WriteLine("Hello"+" "+this.GetType().Name.ToString()); Console.WriteLine("Hello"+" "+ob.GetType().Name.ToString()); } public void Hello2(object ob) { Console.WriteLine("Hii"+" "+this.GetType().Name.ToString()); Console.WriteLine("Hii"+" "+ob.GetType().Name.ToString()); } } // Class B inherits A. class B : A { public void Bmethod() { } } // Class C does not inherit A. class C { } // Because of the base class constraint, all type arguments // passed to Test must have A as a base class. class Test<T> where T : A { T obj; public Test(T o) { obj = o; } public void SayHello() { // OK to call Hello() because it’s declared // by the base class A. obj.Hello2(obj); obj.Hello(this); // Hello(); } } public class Program { public static void Main(string[] args) { A a = new A(); B b = new B(); C c = new C(); // The following is valid because A is the specified base class. Test<A> t1 = new Test<A>(a); t1.SayHello(); // t1.Addnum(5,10); // The following is valid because B inherits A. Test<B> t2 = new Test<B>(b); t2.SayHello(); // The following is invalid because C does not inherit A. // Test<C> t3 = new Test<C>(c); // Error! // t3.SayHello(); // Error! } } }
run
|
edit
|
history
|
help
0
W.I.P (c#)
Pyramid using C#
merge 2 sorted array
7. Asynchrony
c++
Intuit // C# // Lecture_4 // Laba_#2
//Json Get Field from Array
a6
sleep thread
Leapyear