Contrary to what many people think about the dynamic
it does not make variables type-less, this keyword only indicates that the compiler should not check their type. C# is a static typing language (this is the correct term, there is also a lot of confusion about it) and the variables can only have one type. What can happen is that the variable can have its type changed, and precisely for this reason the compiler check needs to be turned off. This can be checked with this code:
dynamic x = 1;
WriteLine(x.GetType());
x = "oi";
WriteLine(x.GetType());
Behold demonstration in .NET Fiddle. Also put on the Github for future reference.
If in C# I have a variable that accepts any value, in which situation it should be used?
It was created for interoperability with other languages that have dynamic typing and with external Apis (by COM, for example) that do not type their data. It was made for use with reflection in some cases where dynamism is important. An example is in the question How to create properties dynamically in C#? or Get values from a dynamic list. Has a question showing the different ways to deal when you don’t know exactly what the type is and dynamic
should be avoided.
In a way it loses the security of types since the compiler is prevented from doing some checking. Several things are Tricky (complicated) to use when abusing dynamic
. This already happens in dynamic typing languages, but people get used to it. When you mix the two things, it gets very complicated. Most C# features were designed for static typing, when a dynamic type enters some things may not work as expected, although the language creators have done a good job of mitigating most cases that would look weird.
And why does the language not only use it as a type of variable since it accepts all values?
It is a matter of philosophy of the language to mater the security of types. Once you start using it, it becomes your responsibility to make sure the guy is always right. Or by observing the code or even writing code that guarantees it. If you use something wrong you will get an exception due to programming error that ideally never occurs. There has to be much more code.
Alternatives
Already the var
was created so that the type can be inferred, that is, it works normally as any type, only that it does not need to write the name of the type in the declaration, the compiler discovers alone.
You can also use the type object
, the variable of this type can receive values of any type because all types are descended from object
. Only you’d have to do casting for accessing members of the actual type of the object. The compiler protects you from accessing members other than the declared type. If you try to access directly by type object
will only be able to access the members of that kind. It is not ideal. The dynamic
has the advantage precisely by preventing the compiler from checking if members are available, it accepts everything.
Proper language
If you think that using this feature is beneficial for widespread use in language - many people don’t - then maybe C# isn’t the best language for you. If you want to continue on . NET would be better to use VB.NET (is not so dynamic) or Ironpython (unsupported), just to give two examples. But to tell the truth if you really want something dynamic, I think it’s best to forget the . NET, you can use it, but it wasn’t thought to use this way.
To use C# you need to identify with static typing. It has advantages and disadvantages. You have to choose what you want. C# put this facility to a specific goal, but to change the philosophy of language. I myself thought I could use it in a more dynamic way since my whole life I used more languages with dynamic typing, but I realized it would be a mistake, or does as the language was designed or gives up on it.
I advise you to be careful with the use of
dynamic
, this is a powerful resource that theC#
possesses (precisely because it is strongly typed), but one should use it carefully. It has very specific cases to be used, such asDynamic Dispatch
, communication withViews
(ViewBag
ofASP.NET MVC
), communication with other technologies that cannot be well mapped in C# and/or need a lot of flexibility in changing contracts (SignalR
with theirHubs
)...– Gabriel Katakura