Why are the other types of variables not replaced by Dynamic in C#?

Asked

Viewed 319 times

17

I was studying C# and I came across a type of variable dynamic, which, as I understand it, accepts any value I enter. Unlike other variables, for example:

If I declare how int, I have to type one valor inteiro.

If I declare how double, I have to type valores quebrados.

I can’t just declare name as int and assign "Meu nome" for the variable. Example:

int nome = "Meu nome";

However, the variable dynamic has a very cool dynamic, for example:

dynamic nome = "Meu nome";
dynamic idade = 20;

My question is: If in C# I have a variable that accepts any value, in what situation should it be used? And because language does not uses only it as a type of variable since it accepts all values?

  • 1

    I advise you to be careful with the use of dynamic, this is a powerful resource that the C# possesses (precisely because it is strongly typed), but one should use it carefully. It has very specific cases to be used, such as Dynamic Dispatch, communication with Views (ViewBag of ASP.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 their Hubs)...

2 answers

20


One of the characteristics of the C# language is to be static typing, this ensures that each type is used only where it is expected.

The guy Dynamic was created to simplify access to COM Apis. In most situations it behaves as the type Object.

While the other types are checked during compilation, the type Dynamic is only verified during execution.

The advantage of checking during compilation is that the programmer is immediately warned of possible misuse of the type.

You can find a complete list of the advantages of static typing in this excellent reply given in the question What is the difference between a static and dynamic programming language?.

18

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 think I missed my answer at compile time, maybe :p. I remove until the answer, not worth leaving yours and mine at the same time, yours is more complete too. + 2

  • 1

    @Brunocosta has no reason to delete an answer unless it is wrong. All information is useful, even as a complement. Another view, another way of expressing can help the person understand better.

  • 2

    @Bigown the coolest is the intelligence that the language has, to distinguish the types of values that are being assigned in the variables of type Dynamic. I ran the example you put on dotNetFiddle, and I thought it was really cool.

  • 1

    This is important, the values continue to have fixed type, the variable is that it can receive a different type object in it and the compiler says nothing, just waiting for you to know what you are doing.

  • @Bigown ball show your explanation. And I agree with you, as I said, if you don’t identify with the language it’s better to change the language, we have to know the language, apply good practices and work in the way that the language was designed to have good results, in case we work in the right way.

  • 2

    I identify with the language, but as I’m starting programming I’m trying to better understand everything I need to know so I can really choose. Now I understand better. rs

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.