Tutorial by Examples

dynamic foo = 123; Console.WriteLine(foo + 234); // 357 Console.WriteLine(foo.ToUpper()) // RuntimeBinderException, since int doesn't have a ToUpper method foo = "123"; Console.WriteLine(foo + 234); // 123234 Console.WriteLine(foo.ToUpper()): // NOW A STRING
using System; public static void Main() { var value = GetValue(); Console.WriteLine(value); // dynamics are useful! } private static dynamic GetValue() { return "dynamics are useful!"; }
using System; using System.Dynamic; dynamic info = new ExpandoObject(); info.Id = 123; info.Another = 456; Console.WriteLine(info.Another); // 456 Console.WriteLine(info.DoesntExist); // Throws RuntimeBinderException
The following output equivalent results: class IfElseExample { public string DebugToString(object a) { if (a is StringBuilder) { return DebugToStringInternal(a as StringBuilder); } else if (a is List<string>) { ...

Page 1 of 1