Basically, it should not be avoided. You have to know as use and when wear. This story of being "good practice" or "bad practice" is, mostly, a trick used to impose rules without having to base what you’re saying.
Because the use of dynamic
is considered a practice to be avoided?
Only those who told you this can answer this question. But it is very likely because, using dynamic
, everything is solved at runtime and not compilation, so it is much easier to do some nonsense and end up breaking the execution of the application.
Switching kids, you simply "throw away" one of the great advantages of static languages which is to know beforehand all the (possible) existing members of an object.
A small example:
dynamic pessoa = new { Nome = "jbueno" };
var n = pessoa.nome;
This code compiles normally, but bursts an error at runtime because the property nome
does not exist in pessoa
.
There is something that can replace the use with the same effect?
If by "effect" you mean dynamism: the answer is no. And if this is really necessary I would even tell you that you’re probably using the wrong language.
For other cases it is possible to give you some tips knowing the real need. Citing all possibilities is unfeasible.
Which case where the use of dynamic
it is necessary?
It depends a little. It is necessary whenever you cannot know beforehand the structure of the object.
A real example would be to request a webservice whose return may have two completely different structures.
It is very likely that there are other cases where it is most useful. I find it difficult that the creation of this resource has been to solve cases like this example.
{ "sucesso": "true" };
{ "erro": "Algo deu errado" };
Related: Difference between Object, Dynamic and var
– Jéf Bueno
What was the use of Dynamic in your code?
– Leo Longhi
Related: Why other types of variables are not replaced by Dynamic in C#?
– Jéf Bueno
@Leolonghi was in a method that could take both numbers and strings, concatenating the result in string cases or summing in number cases, so the output must have been something that was changeable.
– Paz
@Peace this your method already seems to me a
anti-pattern
– Tobias Mesquita
@Tobiasmesquita do not know what is an anti-pattern, I will look for
– Paz
interesting, I don’t know c#, but after looking at the answers below, I come to the conclusion that Dynamics is comparable to a javascript Feature where you can make an object without properties (var a = {}) and assign properties at runtime by making the.property or the['property']. From an object-oriented point of view, this is bad, because we’re assuming that an object is not typed and this kind of forces another programmer to have to read an entire piece of code to try to understand the definition of an object (and is still susceptible to misunderstandings).
– wryel
@Paz https://answall.com/q/189442/101 I tried to answer as completely as possible, but I don’t know why people aren’t seeing this. there is something wrong or missing in my answer?
– Maniero
@bigown is much more complete than the answers I found on the web, in fact it seems to me a compiled of everything I found, really exceptional. + 1
– Paz
@wryel 1) Your comparison with Javascript is mistaken and would be more comparable to "anonymous" types. JS has dynamic typing, the correct comparison would be with the variables and behavior of the objects. 2) The object is typed as
dynamic
, the value of the object that has different types.– Andre Figueiredo
– Andre Figueiredo
Imagine deserializing a polymorphic JSON without Dynamic.
– user178974
@Edney but then we couldn’t use an anonymous Object?
– Paz
Also, normally this restriction is an administrative issue, strong typing ensures that there will be no type error, since the code. In production it is undesirable to depend on Dynamic, but for debugging and development (when you have a more controlled environment) it is a very useful resource. Not to mention that Dynamic generates additional type resolution processing. I had teachers who condemned the use of goto, many see goto and already say it is spaghetti code, in c# it is difficult to make tail recursion without goto, it is more or less the same thing.
– user178974