Skip to content

Instantly share code, notes, and snippets.

@gulbanana
Created August 9, 2016 07:20
Show Gist options
  • Save gulbanana/7c67f3c9c8bb1c1283266dbf8e4e0689 to your computer and use it in GitHub Desktop.
Save gulbanana/7c67f3c9c8bb1c1283266dbf8e4e0689 to your computer and use it in GitHub Desktop.
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