[csharp] buh

Viewer

  1. // Assignment compatibility.
  2. string str = "test";  
  3. // An object of a more derived type is assigned to an object of a less derived type.
  4. object obj = str;  
  5.   
  6. // Covariance.
  7. IEnumerable<string> strings = new List<string>();  
  8. // An object that is instantiated with a more derived type argument
  9. // is assigned to an object instantiated with a less derived type argument.
  10. // Assignment compatibility is preserved.
  11. IEnumerable<object> objects = strings;  
  12.   
  13. // Contravariance.
  14. // Assume that the following method is in the class:
  15. static void SetObject(object o) { }
  16. Action<object> actObject = SetObject;  
  17. // An object that is instantiated with a less derived type argument
  18. // is assigned to an object instantiated with a more derived type argument.
  19. // Assignment compatibility is reversed.
  20. Action<string> actString = actObject;

Editor

You can edit this paste and save as new:


File Description
  • buh
  • Paste Code
  • 24 Mar-2023
  • 846 Bytes
You can Share it: