Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Thomas <lunchtimemama@gmail.com>2009-02-04 07:22:19 +0300
committerScott Thomas <lunchtimemama@gmail.com>2009-02-04 07:22:19 +0300
commitb254c9e9e90e6792929a19ec93d0464ce451e2a6 (patch)
tree808c1b3984b740a8e019a31d0c2e6ad08aaf7e54 /mcs/tests/gtest-variance-1.cs
parentf908a8a6c31cd09050a0c5a36a105b67e463f274 (diff)
This commit adds generic variance support under the new
"future" langversion. Variance is allowed in interface and delegate types. Covariant type parameters are denoted with the "out" keyword like so: interface IFoo<out T> { T Bar { get; } } And contravariant type parameters are dentoed with the "in" keyword, like so: delegate void Bar<in T> (T input); Covariant type parameters can only be used as method return types and type arguments for inherited interfaces. Contravariant type parameters can only be used as by-value method parameter types. Only reference type are variant. !!!IT SHOULD BE NOTED!!! That the Mono VM does not yet fully support generic type variance, so variant code compiled with this feature will not run on Mono until variance support is complete. Code compiled with this feature will run on the .NET 2.0 virtual machine. This patch is contributed under the MIT/X11 license. svn path=/trunk/mcs/; revision=125647
Diffstat (limited to 'mcs/tests/gtest-variance-1.cs')
-rw-r--r--mcs/tests/gtest-variance-1.cs34
1 files changed, 34 insertions, 0 deletions
diff --git a/mcs/tests/gtest-variance-1.cs b/mcs/tests/gtest-variance-1.cs
new file mode 100644
index 00000000000..04fac5216fd
--- /dev/null
+++ b/mcs/tests/gtest-variance-1.cs
@@ -0,0 +1,34 @@
+// Compiler options: -langversion:future
+
+interface IFoo<out T>
+{
+ T Bar { get; }
+}
+
+class Foo : IFoo<string>
+{
+ readonly string bar;
+ public Foo (string bar)
+ {
+ this.bar = bar;
+ }
+ public string Bar { get { return bar; } }
+}
+
+public class Test
+{
+ static int Main ()
+ {
+ string bar = "Who is John Galt?";
+ IFoo<string> foo = new Foo(bar);
+ IFoo<object> foo2 = foo;
+ if (!foo2.Bar.Equals (bar))
+ return 1;
+
+ foo2 = new Foo(bar);
+ if (foo2.Bar != bar)
+ return 2;
+
+ return 0;
+ }
+}