Intro
Now we have introduced main dynamic features of C# and DLR it´s time for see a collection of samples which represent valid scenarios for dynamic C#. Along these usages we will see how dynamic can help to reduce the number of lines of code and provide more readability in general; final code will be more legible and compact. As we have already commented dynamic C# is not intended to replace your common static C# code but it can be very useful for making code cleaner and simpler in specific scenarios, let´s see some of them:
Unify Numeric Methods Sample
There is no common math interface for basic operations when working with numeric types. If we want to provide a class for doing basic math operations we commonly need to provide a specific overload for each type of input: short, integer, long, double, etc…
static class SampleMath
{
public static int Prod(int x, int y)
{
return x * y;
}
public static long Prod(long x, long y)
{
return x * y;
}
public static double Prod(double x, double y)
{
return x * y;
}
//etc....
}
First impression is that we can solve this situation by providing something like:
public static T Prod<T>( T x, T y)
{
return x * y;
}
But this is far from solving the problem, when we try to compile we get following error

Now we will try to use dynamic C# for solving this, first we try the following implementation:
public static dynamic Prod(dynamic x, dynamic y)
{
dynamic res= x * y;
return res;
}
And we can code dummy sample of Main () static method in the following way:
static void Main(string[] args)
{
int x = 12;
int y = 5;
double h = 2.056;
double g = 4.21;
Console.WriteLine(string.Format("Multiplying int=[{0}], int=[{1}], result=[{2}]", x, y, DynMath.Prod(x,y)));
Console.WriteLine(string.Format("Multiplying double=[{0}], double=[{1}], result=[{2}]", h, g, DynMath.Prod(h,g)));
Console.ReadLine();
}
But, previous dynamic implementation of Prod method has a problem with type safety that can raise runtime errors, in order to solve this we can finally use:
public static T Prod<T>(T x, T y)
{
dynamic res = (dynamic)x * y;
return res;
}
COM Interop Sample
Dynamic C# can also be useful when working with COM components. It allows working with COM elements without having to include interop lib references in our project. The syntax becomes clear and simpler. Even more, we can use dynamic C# for providing a trick and being able to provide a single implementation for opening a word document for multiple versions of office – like 200,2003,2007 and 2010.
Let´s see a dummy sample about open Word document at specific local path by using dynamic C#.
static void Main(string[] args)
{
try
{
//dynamic part : creating application
Type wordType = Type.GetTypeFromProgID("Word.Application");
dynamic word = Activator.CreateInstance(wordType);
word.Visible = true;
//open word document
string path = @"C:\Activities\DynWord.docx";
object readOnly = true;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
var _wordDocument = word.Documents.Open(path, missing,
true, missing, missing, missing,
missing, missing, missing, missing,
missing, isVisible);
Console.WriteLine(string.Format("Word document at {0} opened", path));
Console.ReadLine();
}
catch (Exception ex)
{
// exception handling here!
throw new Exception("review word instalation or configuration");
}
}
When running the console application word document at local path is open:


In the next article I´ll show more dynamic C# samples
See you on the road !!