开发者

Namespaceless C# Code?

Every single piece of C# I have ever seen is always wrapped 开发者_开发百科is a namespace. Is it possible to include code that is not wrapped in namespace?


You don't have to declare a specific one. Delete the namespace at the top of the script (along with the opening and closing brackets). This will make it part of the global namespace.

From the spec:

Namespaces and types that have no enclosing namespace are members of the global namespace. This corresponds directly to the names declared in the global declaration space.

Namespaces and types declared within a namespace are members of that namespace. This corresponds directly to the names declared in the declaration space of the namespace.

So technically, it is part of a namespace (the global one) even though you didn't specify it.


Of course, you can define types in the global namespace:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("I have no namespace");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜