Created
August 9, 2016 07:20
-
-
Save gulbanana/7c67f3c9c8bb1c1283266dbf8e4e0689 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class A {} | |
class B : A {} | |
class Badarray | |
{ | |
static void Main(string[] args) | |
{ | |
var x = new A[2]; | |
x[0] = new A(); // good, yes? | |
x[1] = new B(); // also useful | |
Ohno(x); //despite the name this is fine | |
var y = new B[1]; //but now.. | |
x[0] = new B(); //perfectly legitimate; however: | |
Ohno(y); //oh no | |
} | |
static void Ohno(A[] thisArrayContainsAs) | |
{ | |
Console.WriteLine(thisArrayContainsAs[0]); //fine | |
thisArrayContainsAs[0] = new A(); //OH NO | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment