Run Code
|
Code Wall
|
Users
|
Misc
|
Feedback
|
About
|
Login
|
Theme
Covariance & Contravariance
//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 { public class Program { public static void Main(string[] args) { /// ---Assignment compatibility--- string str = "test"; /* An object of a more derived type is assigned to an object of a less derived type. */ object obj = str; Console.WriteLine("obj={0}",obj); /* Cannot implicitly convert type 'object' to 'string'. An explicit conversion exist. */ string str2 = (string)obj; Console.WriteLine("str2={0}",str2); /// ---Covariance共變數--- IEnumerable<string> strings = new List<string>(){"123","abc"}; /* An object that is instantiated with a more derived type argument is assigned to an object instantiated with a less derived type argument. Assignment compatibility is preserved. */ IEnumerable<object> objects = strings; foreach(var o in objects) { Console.WriteLine("object={0}",o); } /// ---Contravariance反變數--- Action<object> actObject = delegate(object o){Console.WriteLine("object={0}",o);}; /* An object that is instantiated with a less derived type argument is assigned to an object instantiated with a more derived type argument. Assignment compatibility is reversed. */ Action<string> actString = actObject; actString("hello~"); } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
ss
Concatenate string with null.
D11
remove duplicates
P.I. Works
Added Carnival, List Matching
AlphabeticalRank
Fórum ➡ Aggregating 2 compatible dictionaries, using LINQ ♦
C# Dictionary with Enum Keys And Values
Testing for Integers and Floats in a String
Please log in to post a comment.