In C# there are no global variables as you ure using and declaring in VB.
I put here some comments about this question.
In C# evrything is a type ans there are two kinds of types:
.value types and
.reference types.
Value types include Predefined Types (e.g., char, int, and float), enum types, and struct types.
Reference types include class types, interface types, delegate types, and array types.
C# provides a “unified type system”.
All types—including value types—derive from the type object.
It is possible to call object methods on any value, even values of “primitive” types such as int.
Example :
2004.ToString(); // is a valid call
In a class you can define :
-fields (members variables associated with an object or class)
-properties (members that provides access to a characteristic of an object or a class )
-methods (members that implement a computation or action that can be performed by an object or class)
*A member cannot be defined ouside of a class. So, such "global variable " cannot be declared outside of a class.
Each member has an associated accessibility, which controls the regions of program text that are able to access the member. There are five possible forms of accessibility:
public
protected
internal
protected internal
private
An object
*Members of a class are either static members or instance members.
Static members are belonging to class and instance members are belonging to objects of that class (instances of classes).
A static field is not part of a specific instance; instead, it identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field for the associated application domain.
Here we can say that a static member is a "global variable" for all objects of a given type.
In C# , an object is created using the new operator.
Example:
class MyClass
{
public MyClass(){}
public static int x;
public int y;
}
void Main()
{
MyClass c1 = new MyClass();
c1.x = 10; // Error, cannot access static member through instance
MyClass.y = 20; // Error, cannot access instance member through type
MyClass.x = 2004; // Ok
}
*What about application domain ?
An assembly that has an entry point is called an application. When an application is run, a new application domain is created.
Several different instantiations of an application may exist on the same machine at the same time, and each has its own application domain.
An application domain enables application isolation by acting as a container for application state.
An application domain acts as a container and boundary for the types defined in the application and the class libraries it uses. For instance, each application domain has its own copy of static variables for these types.
Example:
An empty string is defined as static readonly field in the string class.So every time you need a zeo length string you can say:
string myVar = string.Empty;
Because Empty is declared readonly , you cannot modify it :
string.Empty = ""; // Error
But the above example with MyClass.x , you can modify it at run time.
I hope you understand these features.
-obislavu