Here I wrote one of my first experimentations with dynamic upon beginning Chapter 19 of C# in a Nutshell. With every page I read I am more impressed. In learning a little more, I made this change to the Adder class I wrote before:
namespace Whatever.Objects
{
public static class Adder
{
public static T Add<T> (T originalValue, T addendum)
{
dynamic combo = (dynamic) originalValue + addendum;
return (T) combo;
}
public static string Add(string originalValue, string addendum)
{
string combo = originalValue + " " + addendum;
return combo;
}
}
}
Now concatenation will end up with hello world in it instead of helloworld without a space, well, that is if the code in my example could compile. This is pretty amazing! When using dynamic to resolve a type, dynamic will fall over to more specific types if such types are available!
No comments:
Post a Comment