Saturday, February 27, 2016

Global keyword in C#

The global contextual keyword, when it comes before the :: operator, refers to the global namespace, which is the default namespace for any C# program and is otherwise unnamed.
namespace ConsoleKeyword
{
    class System
    {
        System()
        {

        }
        static void Main(string[] args)
        {
           System.Console.WriteLine("Amit");
        }
    }
}

Whenever you compiled this program you will be get an error like Error 1  'ConsoleKeyword.System' does not contain a definition for 'Console'
In this case we the global keyword to differentiate the class name System and namespace name System.
namespace ConsoleKeyword
{
    class System
    {
        System()
        {

        }
        static void Main(string[] args)
        {
            global :: System.Console.WriteLine("Amit");
            global :: System.Console.ReadKey(true);
        }
    }
}
Output

 Img-1.jpg

No comments:

Post a Comment