Home » .Net

The 'using' Keyword in C#

In this article, we are going to learn to use ‘using’ keyword in C#. What is ‘using’ keyword, how and when it is used in a C# program?
Submitted by IncludeHelp, on August 11, 2018

Prerequisite: Namespace in C#

If you want to include namespace in a program then we need to use using keyword. For example we use Console class which is defined in System namespace that’s why we need to include System namespace using using keyword.

If we want to use Console class without include then we can also access it with the help of . (dot) operator like that:

System.Console.WriteLine("Hello World");

Example:

using System;
using System.Collections;

using namespace1;
using namespace2;

namespace namespace1
{
    class ABC
    {
        public void fun()
        {
            Console.WriteLine("Inside Namespace1");
        }
    }
}

namespace namespace2
{
    class XYZ
    {
        public void fun()
        {
            Console.WriteLine("Inside Namespace2");
        }
    }
}    
    
class Program
{
    static void Main()
    {
        ABC OB1 = new ABC();
        XYZ OB2 = new XYZ();

        OB1.fun();
        OB2.fun();

    }
}

Output

    Inside Namespace1
    Inside Namespace2

Read more: Nested Namespace in C#



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.