Hello. 1
In this video, we'll find an answer to the question 4" What is the purpose of the "dynamic" keyword? 2
Before we understand the meaning of the "dynamic" keyword, we must first understand the difference between 3
static and dynamic typing. 4
C# is a statically-typed and strongly-typed programming language. 5
The opposite of statically-typed is dynamically-typed. 6
Let's see the difference. In Python, which is a dynamically-typed programming language 7
I can do something like this. 8
In this short script, two things happen that could never happen in C#. 9
We change the type of the "hello" variable during the program execution. 10
At first it is a string and then it becomes an int. 11
The parameter of the toUppercase method does not have the type defined, even if the body of the method 12
strongly indicates that it should be a string. From the compiler point of view 13
it is perfectly fine to first call this method with the string parameter and then with the int parameter. 14
Let's see the result of this program. 15
First of all, the program compiled correctly, which would not happen in C#. In C# we would 16
have to declare the type of the parameter of the toUppercase method. We would set it to string 17
and then, if we tried to call this method with an int parameter, the program would not compile. 18
As we can see in the first line of the output 19
the method call with the string parameter worked correctly and the uppercase result was presented 20
to the console. 21
But the second call, the one with the integer parameter caused a runtime error. 22
And this is the essence of the difference between statically-typed and dynamically-typed programming 23
languages. In statically-typed languages like C# all type checks happen at compilation time. In dynamically 24
typed languages like Python type checks happen at runtime. 25
There is no universal answer to which is better. 26
Let's see some advantages and disadvantages of both of those typing methods. In statically-typed languages 27
will have fewer runtime errors. 28
We don't need any code to handle type mismatch, as well as tests that would make sure that it is 29
handling is correct. 30
It is easy to understand what exactly we need to pass to a method to make it work as expected. In dynamically 31
typed languages 32
it can often be confusing. 33
The disadvantages of statically-typed languages are: we must always be aware of the variable types. 34
The language is more rigid, type declarations take place and clutter the code. 35
The compilation is longer. 36
Finally, methods are tied to specific types, and they can't be easily made to work with other types 37
as well. 38
All right. 39
We now know that C# is a statically-typed language and what are the consequences of this fact. 40
If you are used to working in C#, you probably don't ever suffer from the lack of dynamic typing. 41
But what if we really, really wanted to use dynamic typing in C# for some reasons? 42
Well, in this case, the "dynamic" keyword comes in handy. 43
A variable declared with the dynamic type will bypass the static type checking. 44
In other words, it will behave as it belonged to a dynamically-typed language. 45
Let's see this in practice. 46
As you can see, I declared a variable of dynamic type and assigned it a string. 47
Then I called a method on this object. 48
This method does not exist in string type. 49
If the "text" variable was declared as string, the code would not compile, but it is declared as dynamic 50
so the compiler doesn't execute type checking. 51
I will be able to compile and run this program. 52
But of course, it will fail at runtime. 53
This is what we talked about when defining a difference between static and dynamic typing. With dynamic 54
typing 55
we exchange to compile-time errors for runtime errors. 56
The result of most of the operations involving dynamic type is also the dynamic type. 57
In this case, the code will not fail at runtime because the ToUpper method exists in the string 58
type. 59
As you can see, the "toUpper" variable is also of dynamic type. 60
There are only two cases when operations involving dynamic types do not result in dynamic types. 61
First, when casting from dynamic type to another type. 62
This would fail if the dynamic text variable did not hold a string. 63
The second operation that involves dynamic types, but does not result in a dynamic type, is when having 64
a constructor calls using dynamic as parameters. In this class 65
there is a constructor with a dynamic parameter. 66
The "someClass" variable is not dynamic, but of SomeClass type. 67
All right. 68
We now know how dynamic types work. 69
So what can be the use case for them? 70
Well, if you want to use them just to make C# more "Python-y", please don't. 71
Unless you do it for fun or out of curiosity in a non-production code. Mixing static typing, which 72
is used in C# with dynamic typing, is a risky business, and it will most likely result in code 73
you can't maintain for long. Unless you are 100 percent sure what you're doing and you have a rock-solid 74
suit of unit tests, 75
this is most likely a bad idea. But there are cases when the type you are given is simply unknown in 76
our project. 77
But we, as programmers, actually know what it is. 78
Sounds weird, 79
so let me give you an example. 80
As you probably know, C# can cooperate with other .NET-compatible languages. 81
And what if such a language is dynamically-typed? 82
An example can be IronPython, which is a .NET-compatible implementation of Python. 83
We can actually execute IronPython code from C# code, but the result of this call will not be 84
something C#can understand. 85
We can assign such a result to a dynamic variable. 86
Let's see this in practice. 87
In this method, we define an IronPython script in which we define class PythonClass with one method 88
toUpper. 89
This method simply takes a variable and makes it uppercase. 90
Here we create an instance of the PythonClass, and here we call the toUpper method on this instance. 91
This is possible because the instance is dynamic. 92
C# doesn't understand that the toUpper method exists in this object, but since it's dynamic, 93
the compiler doesn't complain. 94
We, as programmers, know that this method is there because we defined it in the Python code. 95
We take the risk of runtime error if we make a mistake and, for example, make a typo like this. 96
The result of the RunPython method is also dynamic because, again, the C# compiler doesn't know 97
what will be the result of a call of any Python method since Python is dynamically-typed. 98
Another example of using the "dynamic" keyword to work with unknown types is using COM Objects. COM stands for 99
"Component Object Model", and it's a binary interface standard for Windows software components. 100
In simple terms, a COM object is something that can be understood by different Windows programs. 101
And, for example, it can allow communication between Excel and C# programs. 102
But then, if we execute some method on a COM object from the C# code, 103
the result type will be unknown to the C# compiler. 104
It will again be a case when declaring the result as dynamic will be useful. 105
I don't want to get into too much detail on this topic because the most useful COM objects are related to 106
the Microsoft Office software, which is not free to use. 107
If you are curious, you can check the article I linked in the document attached to this lecture. 108
Before we wrap up, I want to explain one thing I mentioned in this lecture, but did not go into details. 109
I said that C# is statically-typed, strongly-typed programming language. 110
I explained the difference between static and dynamic typing, but what is strong and weak typing? 111
Let's consider a simple operation like this. 112
Please note that this is a pseudo-code, not any real language. In strongly typed languages 113
this would raise an error. 114
For example, C# and Python are strongly-typed languages. In weakly typed languages like Perl 115
the result would be 10. In weakly-typed languages 116
variables are automatically converted from one type to another. In strongly-typed languages 117
this is not the case. 118
Let's summarize .The "dynamic" keyword allows us to bypass static type checking that is done by default 119
by the C# compiler. 120
We can call any operations on dynamic variables, and the code will still compile. 121
Whether this operation is available in the object or not will only be checked at runtime. 122
The "dynamic" keyword is most useful when working with types unknown in our codebase. 123
Like dogs being the result of dynamically typed languages scripts or operations called on COM objects. 124
During the interview, you may be asked questions like: "What is the difference between strongly-typed 125
and weakly-typed programming languages?" In weakly typed languages 126
variables are automatically converted from one type to another. In strongly typed languages 127
this is not the case. 128
"What is the difference between statically-typed and dynamically-typed programming languages?" In statically 129
typed languages 130
the type checks are done of the compile-time, while in dynamically-typed languages they are done at runtime. 131
For example, in C#, we can't pass an integer to a method expecting a string. In Python, which is 132
dynamically=typed we can 133
but the execution would result in a runtime error, 134
if in this method, I would call some operation that is not available in the int type. 135
"What are COM objects?" 136
COM stands for Component Object Model, and it's a binary interface standard for Windows software components. 137
In simple terms, a COM object is something that can be understood by different Windows programs, 138
And, for example, it can allow communication between Microsoft Office suite and C# programs. 139
All right. 140
That's it for this lecture. 141
Thanks for watching, and I'll see you in the next one.