Let’s come to basics of programming today. It’s a common interview question asked during an interview and many time candidates become confused with the difference between using directive and using statement/block. Today we will cover this topic and discuss various difference among those.
To learn or brush up the basic concepts, continue reading more. I hope, this will clarify all your doubts. If any feedback, do share below in the comments section.
“using” directives in C#
The “using directive” has three different uses. In one way, you can use it to declare the type in a namespace once and access that type without writing the fully qualify name in all other places. In this case, suppose you declare a type “System.IO” in a namespace, then you don’t have to write the fully qualified name of classes present in the same namespace.
For example, if you declare “using System.IO;” in a namespace, you don’t have to write “System.IO.FileStream” or “System.IO.MemoryStream”. Instead of that, you can directly write “FileStream” or “MemoryStream”, which will automatically qualify the class.
The using directive also allows you to access a static member of a type without writing the fully qualified name in all other places in a single namespace or code path.
For example, if you declare using directive “System.Math” as a static member type, you don’t have to write “System.Math” every time to call it’s static methods. You can directly call “Round” or “Sqrt” methods instead of writing the whole namespace multiple times.
As a third approach, you can create an alias for a namespace or a type. This is well known as “alias directive” which can be created as shown here: “using Store = Windows.Storage”. This way you can also reduce the length of namespace declaration.
The scope of using directive is limited to the file in which it has been declared. Use it to make it easier to fully qualify the namespace once in a code file and use the reference in all the places within the same code file.
“using” statement or block in C#
You can also use the keyword “using” to declare a block, which will ensure that IDisposable objects are released properly when it comes out from the block. The “using block” can be only created for the objects which implements IDisposable interface. Here’s how you can use it:
using (FileStream fileStream =new FileStream(filePath, FileMode.OpenOrCreate))
using (MemoryStream memoryStream = new MemoryStream())
You might also like to read:
- What is Yield Return? Let’s have fun with it
- Difference between the ref and out keywords in C#
- Difference between a struct and a class in C#
- What is Duck Typing?
Was the post helpful to you? Do let me know in the below comments section. If you still have a doubt, drop a line and I would be happy to clarify it. Connect with me on Twitter, Facebook and Google+ and subscribe to my news feed to get all the updates that I share over those social networking sites. Also do follow my blog for new articles and tips. That’s all for today. Have a great day and happy coding.