What are the advantages of using 'using' keyword in C# and why would you use it?
Experience Level: Junior
- The keyword
usingis used at the top of the .cs file to tell the computer in which namespaces to look for the classes that are referred to from the file.
- In the first example below, the class
OakTablewill be searched for in the namespaces
- In the second example below, the class
OakTableis defined by the fully qualified name that includes the namespace. You can see that the code for creation new instance of class OakTable in the first example is way shorter than the one in the second example. This is the advantage of using the keyword
using. It helps to reduce the code duplication.
Related C# job interview questions
.NET ASP.NET MVC ASP.NET WebAPI ASP.NET WebForms C# Senior