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

14

I was studying C # and I came across a type of variable dynamic , which from what I understand, it accepts whatever value I enter. Unlike other variables, such as:

If I declare as int , I have to type a valor inteiro .

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

I can not simply declare the name as int and assign "Meu nome" to the variable. Example:

int nome = "Meu nome";

However, the variable dynamic has a very cool dynamic, such as:

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   does it only use it as a variable type since it accepts all values?

    
asked by anonymous 27.10.2016 / 16:49

2 answers

17

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

dynamic is designed to simplify access to COM APIs. it behaves like the Object type.

While the other types are checked during compilation, the dynamic type is only checked 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 full list of the advantages of static typing in this excellent answer given in question What's the difference between a static and dynamic programming language? .

    
27.10.2016 / 16:55
14

Contrary to what many people think about dynamic it does not make the variables untyped, this keyword only indicates that the compiler should not check its type. C # is a static typing language (this is the correct term, there is also a lot of confusion about it) and variables can only have one type. What can happen is that the variable can have its type changed, and that's why the compiler check needs to be turned off. This can be verified with this code:

dynamic x = 1;
WriteLine(x.GetType());
x = "oi";
WriteLine(x.GetType());

View the demonstration in dotNetFiddle .

  

If in C # I have a variable that accepts any value, in what situation should it be used?

It is built for interoperability with other languages that have dynamic typing and with external APIs (for COM, for example) that do not type your data. It was made for use with reflection in some cases where dynamism is important. An example is in the question How to dynamically create properties in C #? or Pick values from a dynamic list . You have a question showing the different ways to handle when you do not know exactly what kind and dynamic should be avoided.

In a way, it loses type security since the compiler is prevented from doing any checking. Several things are tricky to use when abusing dynamic . This already occurs in dynamic typing languages, but people get used to it. When you mix the two things, it gets really complicated. Most C # features were designed for use with static typing; when a dynamic type comes in some things may not work as expected, although language creators have done a good job of mitigating most cases that would be weird. / p>

  

And why does not the language just use it as a variable type since it accepts all values?

It is a matter of the philosophy of the language of mater to type security. When you start using this, it is your responsibility to ensure that the type is always correct. Or by observing in the code or even writing code that guarantees this. If you use something wrong you will get an exception for a programming error that is not ideal. There has to be a lot more code.

Alternatives

var has been created so the type can be inferred , that is, it normally works as all sorts, just do not need to type the type name in the statement, the compiler finds out alone.

You can also use the type object , the variable of this type can receive values of any type because all types are descendents of object . Only you would need casting to access the members of the actual type of the object. The compiler protects you from accessing members that are not of the declared type. If you try to access direct by type object you can only access the members of this type . Not ideal. The dynamic has the advantage precisely by preventing the compiler from checking if the members are available, it accepts everything.

Proper language

If you think using this feature is beneficial to generalized use of language - a lot of people do not think - then maybe C # is not the best language for you. If you want to continue in .Net it would be better to use VB.Net (it's not that dynamic) or IronPython (not supported), just to give two examples. But to tell you the truth if you want something dynamic yourself, I think it's best to forget. Net, you can use it, but it was not meant to be used this way.

To use C # you need to identify yourself 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 even thought I could use it in a more dynamic way since I've been using more languages with dynamic typing all my life, but I realized that it would be a mistake, or how the language was projected, or give it up.

    
27.10.2016 / 17:18