0 1 00:00:06,690 --> 00:00:07,130 Hello. 1 2 00:00:07,530 --> 00:00:13,020 In this video, we'll discuss question 3: "What is the use of the "using" keyword?" 2 3 00:00:13,440 --> 00:00:16,410 The "using" keyword has two main uses. 3 4 00:00:17,040 --> 00:00:23,460 The using directive, which allows using types from other namespaces and to create aliases. 4 5 00:00:23,730 --> 00:00:30,660 And the using statement that defines the scope in which the IDisposable object will be used, and that 5 6 00:00:30,660 --> 00:00:33,030 will be disposed under these scope's end. 6 7 00:00:33,780 --> 00:00:39,420 First, let's talk about the using directive. You are probably familiar with it already. 7 8 00:00:43,510 --> 00:00:49,930 This is a using directive which will allow me to use types from the System.Diagnostics namespace. If 8 9 00:00:49,930 --> 00:00:53,920 it wasn't here, I wouldn't be able to use types like Stopwatch. 9 10 00:00:56,230 --> 00:00:58,420 Now this code doesn't compile. 10 11 00:00:59,320 --> 00:01:04,840 The other use of the using directive is to create aliases for some type's names. 11 12 00:01:05,230 --> 00:01:10,690 This is particularly useful when we have conflicting type names, and we wanted to use them both in 12 13 00:01:10,690 --> 00:01:13,740 a single file. In this project 13 14 00:01:13,840 --> 00:01:18,700 I have two classes named Person, but defined in different namespaces. 14 15 00:01:19,120 --> 00:01:23,170 Here is the Person class defined in namespace DomainObjects, 15 16 00:01:24,400 --> 00:01:28,180 and here another class defined in namespace DTOs. 16 17 00:01:28,480 --> 00:01:32,890 They look almost the same, but nevertheless, they are two different types. 17 18 00:01:33,550 --> 00:01:40,270 If I simply imported both the namespaces in this file and try to create an object of the Person class, 18 19 00:01:40,450 --> 00:01:42,370 the compiler wouldn't know which one. 19 20 00:01:42,370 --> 00:01:43,000 I mean. 20 21 00:01:50,170 --> 00:01:57,670 As you can see, the error says "Person is ambiguous reference between DTO.s Person and 21 22 00:01:57,670 --> 00:01:58,240 DomainObjects.Person. 22 23 00:01:58,720 --> 00:02:02,920 To solve this, I can create type aliases with the using directive. 23 24 00:02:14,740 --> 00:02:22,390 Now I can refer to the Person type from the DTOs namespace by its alias PersonDto. The next topic 24 25 00:02:22,390 --> 00:02:25,480 I want to discuss is the using static directive. 25 26 00:02:25,960 --> 00:02:32,440 It is particularly useful when in a file we use a lot of static methods from a particular namespace. 26 27 00:02:32,860 --> 00:02:37,690 For example, in this code, I use the Console type a lot. 27 28 00:02:38,020 --> 00:02:44,200 I could shorten this code by importing all static methods from the Console class with they're using static 28 29 00:02:44,200 --> 00:02:44,980 directive. 29 30 00:02:48,510 --> 00:02:51,300 Now I can skip the "Console" in my code. 30 31 00:02:53,750 --> 00:02:57,980 The last thing worth mentioning is the global using directive. 31 32 00:02:58,160 --> 00:03:05,750 This feature is available starting with the C# 10. When a type is imported in any file with this directive, 32 33 00:03:05,810 --> 00:03:09,440 it is like it was imported in all files in the project. 33 34 00:03:09,950 --> 00:03:15,120 I'm going to globally import the System.Diagnostics namespace in the Program file. 34 35 00:03:17,060 --> 00:03:23,420 Now, in some other files, I can use types from this namespace as they were imported there too. 35 36 00:03:26,000 --> 00:03:32,240 As you can see, the System.Diagnostic namespace seems not to be imported here, but I can still declare 36 37 00:03:32,240 --> 00:03:35,840 a field of type Stopwatch coming from this namespace. 37 38 00:03:36,230 --> 00:03:39,950 This is because it was globally imported in the Program file. 38 39 00:03:40,980 --> 00:03:47,430 When working on projects in Visual Studio 2022, you will notice something interesting. 39 40 00:03:50,790 --> 00:03:57,330 As you can see, I can use console class, even if the System namespace is not explicitly important. 40 41 00:03:57,810 --> 00:04:01,290 I don't have "using System" at the top of this file. 41 42 00:04:04,860 --> 00:04:06,150 How is it possible? 42 43 00:04:06,480 --> 00:04:09,450 Well, let's take a look at the project settings. 43 44 00:04:11,480 --> 00:04:13,580 And here is an interesting entry. 44 45 00:04:13,850 --> 00:04:19,880 This setting means that global usings will be defined for a couple of the most commonly used 45 46 00:04:19,880 --> 00:04:27,320 namespaces from C# standard library, for example System or System.Linq. To find where those using are 46 47 00:04:27,320 --> 00:04:28,370 actually defined, 47 48 00:04:28,550 --> 00:04:32,660 we must build the project and go to the directory where it exists. 48 49 00:04:36,240 --> 00:04:45,630 Now we must go to the obj/Debug/.net6 folder, and here we will find an autogenerated file. 49 50 00:04:46,350 --> 00:04:47,880 Let's see what's inside. 50 51 00:04:49,110 --> 00:04:52,920 Here are the global usings generated when building the project. 51 52 00:04:53,250 --> 00:04:56,910 Remember, this is only available starting with C# 10. 52 53 00:04:58,240 --> 00:04:58,970 All right. 53 54 00:04:59,230 --> 00:05:03,580 We learned the first use of the using keyword - the using directive. 54 55 00:05:03,850 --> 00:05:06,370 Now let's discuss this using statement. 55 56 00:05:06,850 --> 00:05:13,510 It is used to define the scope in which an IDisposable object will be used and that will be disposed 56 57 00:05:13,510 --> 00:05:19,940 at this scope's end. We will learn more about the Dispose method in the "What is the difference between Dispose 57 58 00:05:19,940 --> 00:05:22,120 and Finalize methods?" lecture. 58 59 00:05:22,570 --> 00:05:25,660 In simple terms, instead of writing this: 59 60 00:05:36,230 --> 00:05:38,000 We can simply write this. 60 61 00:05:44,010 --> 00:05:49,650 Logically, this code is the same, but we don't need to remember about calling the Dispose method 61 62 00:05:49,860 --> 00:05:52,590 and making sure the "finally" clause is there. 62 63 00:05:52,890 --> 00:05:59,820 And because of that, the code is much shorter and there is a smaller chance of making a mistake. Starting 63 64 00:05:59,820 --> 00:06:00,810 with C# 8 64 65 00:06:00,870 --> 00:06:02,850 we can make this code even shorter. 65 66 00:06:07,730 --> 00:06:11,990 Let's summarize, the "using" keyword has two main uses. 66 67 00:06:12,290 --> 00:06:19,490 The using directive, which allows using types from other namespaces, and to create aliases for namespaces, 67 68 00:06:20,060 --> 00:06:25,790 and the using statement that defines the scope in which the IDisposable object will be used 68 69 00:06:25,910 --> 00:06:28,070 and that will be disposed at this scope's end. 69 70 00:06:28,070 --> 00:06:35,540 During the interview, you might be asked "What are the global using directives?" When a type is imported 70 71 00:06:35,540 --> 00:06:38,180 in any file with the global using directive 71 72 00:06:38,240 --> 00:06:41,720 it is like it was imported in all files in the project. 72 73 00:06:42,230 --> 00:06:48,740 This is convenient when some namespace like, for example, System.Linq, is used in almost every file 73 74 00:06:48,740 --> 00:06:49,670 in the project. 74 75 00:06:50,210 --> 00:06:50,840 All right. 75 76 00:06:51,170 --> 00:06:53,060 That's it about the "using" keyword. 76 77 00:06:53,420 --> 00:06:56,090 Thanks for watching, and I'll see you in the next video.