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