0

Why It's not legal having "int" inside a namespace? "int" is a type. right? Just like "class". I know that there are differences between them but there are both Data Types.

namespace Ikco.Crp.UI.Common
{
    public int i;
    ....
    ....
}

What is the Microsoft' idea about that?

odiseh
  • 25,407
  • 33
  • 108
  • 151

3 Answers3

12

There is a difference between defining a class and a variable. When you put a class into a namespace, you define a new type. In your declaration, you are not defining a new type. Instead, you use an existing type (int) to define a variable. The equivalent using classes would be something like

namespace Foo{
  class Bar{}   // type definition
  Bar the_bar;  // variable definition
}

The second definition isn't allowed, either, so classes and ints really behave the same: there are no global variables in C#.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
4

There are no global variables in .NET. I guess that's the idea.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
3

"int" is a type. right? Just like "class". I know that there are differences between them but there are both Data Types.

int is a data type. class is a keyword. The syntax you are using declares a variable inside a namespace, and that is not allowed.

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96