Pages

Saturday, August 21, 2010

The dynamic Type

Example Using dynamic

C# 4.0 introduces a new type called dynamic. In some ways it looks just like any other
type such as int, string, or FileStream: you can use it in variable declarations, or func-
tion arguments and return types, as Example 18-4 shows. (The method reads a little
oddly—it’s a static method in the sense that it does not relate to any particular object
instance. But it’s dynamic in the sense that it uses the dynamic type for its parameters
and return value.)

static dynamic AddAnything(dynamic a, dynamic b)
{
dynamic result = a + b;
Console.WriteLine(result);
return result;
}

No comments:

Post a Comment