tags:

views:

132

answers:

5

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 class. wanted to make sure .

+10  A: 

Yes, that's perfectly legal C# code.

Could you not have tested it out for yourself?

LukeH
then how can we have access to for example E1?- did not work for me in a project - namespace.FileName.E1 ??? - did not work!
BDotA
`Something.Otherthing.E1`
sshow
It's actually `something.otherthing.E1`. C# is case sensitive :)
Igor Zevaka
I just have a habit of CamelCasing namespaces and classes.
sshow
+4  A: 

Yes, you can do that.

Neil Whitaker
+2  A: 

You may only declare the following outside a class...

class, enum, delegate, interface or struct.

Everything else must be in a class.

Carter
+4  A: 

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.

Jouke van der Maas
+1  A: 

The namespace can contain Type definitions. Types are derived from class, struct and enum. interface and delegate are definitions of Type declarations.

As previously mentioned a namespace can be empty.

Philip Smith