What are the advantages of using 'using' keyword in C# and why would you use it?
Experience Level: Junior
Tags: C#
Answer
- The keyword
using
is 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
OakTable
will be searched for in the namespacesSystem
andFurniture.Tables
. - In the second example below, the class
OakTable
is 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 keywordusing
. It helps to reduce the code duplication.
Related C# job interview questions
What is an object in C#?
C# JuniorWhat is a class in C#?
C# JuniorWhat is a namespace in C# and what is it good for?
C# JuniorWhat is abstract class and what is it good for?
C# MediorWhat is covariance and contravariance?
.NETASP.NET MVCASP.NET WebAPIASP.NET WebFormsC# Senior