什么是全球::关键字在C#中使用? [英] What is the usage of global:: keyword in C#?

查看:200
本文介绍了什么是全球::关键字在C#中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是全球:: 关键字在C#中使用?我们什么时候必须使用这个关键字?

What is the usage of global:: keyword in C#? When must we use this keyword?

推荐答案

从技术上说,全球不是关键字:这是一个所谓的上下文关键字。这些有特殊意义只能在有限的程序上下文,可以用来作为背景之外标识符。

Technically, global is not a keyword: it's a so-called "contextual keyword". These have special meaning only in a limited program context and can be used as identifiers outside that context.

全球 CAN每当有歧义或当一个成员是隐藏的,应使用。从这里的href=\"http://msdn.microsoft.com/en-us/library/c3ay4x3d%28VS.80%29.aspx\">:

global can and should be used whenever there's ambiguity or whenever a member is hidden. From here:

class TestApp
{
    // Define a new class called 'System' to cause problems.
    public class System { }

    // Define a constant called 'Console' to cause more problems.
    const int Console = 7;
    const int number = 66;

    static void Main()
    {
        // Error  Accesses TestApp.Console
        Console.WriteLine(number);
        // Error either
        System.Console.WriteLine(number);
        // This, however, is fine
        global::System.Console.WriteLine(number);
    }
}

请注意,但是,全球当类型没有指定命名空间不起作用:

Note, however, that global doesn't work when no namespace is specified for the type:

// See: no namespace here
public static class System
{
    public static void Main()
    {
        // "System" doesn't have a namespace, so this
        // will refer to this class!
        global::System.Console.WriteLine("Hello, world!");
    }
}

这篇关于什么是全球::关键字在C#中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆