Can we have a .cs file in a namespace , but without a class?
in a C# assembly, can we have a file for example File1.cs that is in the same namespace as that assembly but it does Not have a class? so for example something like this:
namespace something.otherthing
{
public enum E1
{ ..... }
public enum E2
{ ... }
}
I think this should be Wrong? but we could do that in VB 6.0 but in C# every thing should be a c开发者_开发技巧lass. wanted to make sure .
Yes, that's perfectly legal C# code.
Could you not have tested it out for yourself?
File names have nothing to do with namespaces. Any namespace that's at the top of the file is where the enums are, so if that's something.otherthing
the enums are available with something.otherthing.E1
. You can have multiple classes in one file, you can have part of a class in a file, you can have no classes at all in a file, you can have enums with classes in a file, etc. Files are just for you, they don't mean anything.
Yes, you can do that.
You may only declare the following outside a class...
class, enum, delegate, interface or struct.
Everything else must be in a class.
The namespace can contain Type
definitions. Type
s are derived from class
, struct
and enum
. interface
and delegate
are definitions of Type
declarations.
As previously mentioned a namespace can be empty.
精彩评论