Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Contravariance
//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 { // This generic interface supports contravariance. public interface IMyContraVarGenIF<in T> { void Show(T obj); } // Implement the IMyContraVarGenIF interface. class MyClass<T,V> : IMyContraVarGenIF<T> { public void Show(T x) { Console.WriteLine(x); } public void Show2(V x) { Console.WriteLine(x); } } // Create a simple class hierarchy. class Alpha { public override string ToString() { return "This is an Alpha object."; } // ... } class Beta : Alpha { public override string ToString() { return "This is a Beta object."; } // ... } public class Program { public static void Main(string[] args) { IMyContraVarGenIF<Alpha> AlphaRef = new MyClass<Alpha,Beta>(); IMyContraVarGenIF<Beta> BetaRef = new MyClass<Beta,Alpha>(); IMyContraVarGenIF<Beta> BetaRef2 = new MyClass<Alpha,Beta>(); //IMyContraVarGenIF<Alpha> BetaRef12 = new MyClass<Beta>(); BetaRef.Show(new Beta()); BetaRef2.Show(new Beta()); AlphaRef.Show(new Alpha()); BetaRef = AlphaRef; BetaRef.Show(new Beta()); } } }
run
|
edit
|
history
|
help
0
Examples of JS-Like higher-order functions in C#
Fibonacci with Given Length
internal access specifier
Herencias
remove special char
Daily Coding Problem: Problem #1
Ftg
a6
Https://www.t.me/Govnoposti
simultQ