odiseh
odiseh

Reputation: 26547

.net: Why It's not legal having int inside a namespace?

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?

Upvotes: 0

Views: 453

Answers (3)

ta.speot.is
ta.speot.is

Reputation: 27214

"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.

Upvotes: 3

Martin v. Löwis
Martin v. Löwis

Reputation: 127527

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#.

Upvotes: 12

Uwe Keim
Uwe Keim

Reputation: 40746

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

Upvotes: 4

Related Questions