Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
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
Необязательные аргументы
Microsoft Code
(Sort)Rounding Magic - C#
⁸
Strings Mix Complete
Unix time stamp
Expression Tree - Demo1
Day1
Program
IsDivisibleV2