Skip to content

Instantly share code, notes, and snippets.

@gulbanana
Created August 10, 2016 02:41
Show Gist options
  • Save gulbanana/c608f1a09145eee45d175e7c139fa3e3 to your computer and use it in GitHub Desktop.
Save gulbanana/c608f1a09145eee45d175e7c139fa3e3 to your computer and use it in GitHub Desktop.
using System;
using System.Dynamic;
class LookImUsingRuby
{
static void Main(string[] args)
{
var x = new { Foo = 1, Bar = "data" }; // anonymous object!
Console.WriteLine(x.Foo); // this is type-checked because within this method, we know Foo exists
PrintFoo(x); // no typechecking beyond this point. it works, though
dynamic y = new ExpandoObject(); // this one doesn't even have a preset structure lol
y.Foo = "???"; // a different type to x.Foo
PrintFoo(y); // and yet this still works
}
static void PrintFoo(dynamic x)
{
Console.WriteLine(x.Foo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment