News & Blog

XITASO Tech Talk: The dynamic type in C#

27.03.2015 | Entwicklerblog

Liutauras Diu presented his insights in the dynamic type in C# as one of the weekly XITASO tech talks. C# 2010 introduced a new type, dynamic. The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object. At compile time, an element that is typed as dynamic is assumed to support any operation. Therefore, you do not have to be concerned about whether the object gets its value from a COM API, from a dynamic language such as IronPython, from the HTML Document Object Model (DOM), from reflection, or from somewhere else in the program. However, if the code is not valid, errors are caught at run time.

Liutauras Diu 
Software developer at XITASO since 2015 

Before Liutauras joined XITASO the native Lithuanian was with Amazon in London for several years. At XITASO he is working in the area “Technical software”.

How did you come about this topic of dynamic type in C#?
Liutauras: When I used C# the last time back in 2009 the dynamic type hadn’t existed yet. In the meantime, for the last 4 years or so, I was mainly using dynamic languages: Perl, Python, Ruby. There I got to appreciate how elegant, concise and expressive a programming language could be. So when I read about the dynamic type in C# while working on our current project in C# 4.0, I got really excited. Further, I had a chat with other team members who use C# way longer than me about the usage of dynamic type in production code. This is how the idea for tech talk was born.

What is the advantage of dynamic languages?
Liutauras: It’s a huge huge topic and I wouldn’t go on stating that dynamic languages have an advantage over static languages or vice versa. These are different concepts with different benefits and drawbacks.

When do you recommend to use dynamic type variables instead of strongly bound ones?
Liutauras: In general, when working in C# you should avoid to use the dynamic type in most of the cases. C# is a statically typed language by design and the reason for introduction of dynamic type was not to make it dynamic. Most likely you will be using it when writing C# code which intercommunicates with COM, for example MS Office. Still, you will never have a real need to use dynamic type in your C# project. However, when you do you will be really happy that it was added. It just simplifies inter communicative code so much.

How about the guys in the audience? Did you personally benefit from this tech talk?
Simon Stimpfle (apprentice at XITASO since 2014): I have never heard about this dynamic type in C# before. thus I learned about an interesting possibility how to handle variables whose type isn’t known a priori. If you have to interact with some code that is written in another language, I guess it is much easier to use the values it returns.

In which respect is this tech talk of relevance for your everyday professional life?
Jan Güntner (software developer at XITASO since 2012): I had an idea about dynamic type in C# before. But since I don’t need it for my current project I’m not very deep in the field. For me, today’s discussion recalled and clarified when I could or should use the dynamic type. This will be a benefit and save time in future project

Take home message

  • The dynamic type is a handy way when your code has to collaborate with dynamic language.
  • Handle the dynamic type with quite some care. Errors are only recognized at run time!

Further reading