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