'using'
keyword is used to define aliases.
Syntax
using
aliasName = namespaceDeclaration;
Example
namespace
NameSpace1
{
namespace NameSpace2
{
namespace NameSpace3
{
namespace NameSpace4
{
class Arithmetic
{
public static int sum(int
a, int b)
{
return a + b;
}
}
}
}
}
}
To
access the method sum of the class ‘Arithmetic’, you need to call the following
statement.
int
result1 = NameSpace1.NameSpace2.NameSpace3.NameSpace4.Arithmetic.sum(10, 20);
Notify
the above statements, it requires more typing to call the method sum. By using
name space aliases, we can solve this problem very easily.
Following
statement define an alias.
using
sumAlias = NameSpace1.NameSpace2.NameSpace3.NameSpace4.Arithmetic;
You
can call the method 'sum' like below.
int
result1 = sumAlias.sum(10, 20);
Following
is the complete working application.
Program.cs
using System; using sumAlias = NameSpace1.NameSpace2.NameSpace3.NameSpace4.Arithmetic; namespace NameSpace1 { namespace NameSpace2 { namespace NameSpace3 { namespace NameSpace4 { class Arithmetic { public static int sum(int a, int b) { return a + b; } } } } } } class Program { static void Main(string[] args) { int result1 = sumAlias.sum(10, 20); Console.WriteLine("Sum of 10 and 20 is {0}", result1); } }
Output
Sum
of 10 and 20 is 30
No comments:
Post a Comment