Global Scope

G

george r smith

In the MSDN documentation there is a reference to "the global scope". For
example
"You can declare types directly in the global scope."

I have search extensively and can not find a definition of the "global
scope" but could it
be the space above the namespace reserved word, such as:

public class Bank { ... }
namespace Banking

Is the public class Bank in the global scope area and what purpose does this
placement
serve.
thanks
grs
 
1

100

Hi george,

In C# when type is defined outside any namespace it becomes part of default
unnamed namespace. This global namespace could be what they reference as
*the global scope*.

Even though C# doesn allow methods and variables to be declared outside the
types CLR support such global methods and variables and they can be used at
least with ILAsm. So it could be the other possible place where you could
find *the global scope* term used. Anyway, I believe these global methods
and variables are not CLI complaint.

HTH
B\rgds
100
 
W

Willy Denoyette [MVP]

Can you please indicate at which page in MSDN there is such reference?

Willy.
 
1

100

Hi George,
I this article you can read:
"...
C# doesn't allow variables at global scope anyway, but it is possible to
create an object on the heap at the outset of a method and not use it until
much later, if at all. The Singleton pattern offers an elegant solution in
such cases.
....
"http://msdn.microsoft.com/library/d...ry/en-us/csspec/html/vclrfcsharpspec_17_2.asp
For attributes *global scope* means that they are defined for the entire
assembly or module.


B\rgds
100
 
W

Willy Denoyette [MVP]

george r smith said:
I did a search with google "global scope" and c# and came up with
http://msdn.microsoft.com/msdnmag/issues/01/07/patterns/
http://msdn.microsoft.com/library/d...ry/en-us/csspec/html/vclrfcsharpspec_17_2.asp

You will also see many mentions of the term. I have had two responses one
says area above the namespace is global scope - the othere says between
namespace and the first class.

George,
Please read both articles carefully.
[1]<Article snip> A variable declared at global scope will be created on startup regardless of whether it is needed—it may very well
be that the object isn't always needed. C# doesn't allow variables at global scope anyway, but it is possible to create an object on
the heap at the outset of a method and not use it until much later, if at all. The Singleton pattern offers an elegant solution in
such cases.

[2] Is talking about attributes, nowhere is stated that "types can have global scope"

The current version of C# doesn't allow globally scoped types (you'll have to wait for the next version).
Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top