Structures , Enumerations and Namespaces

Structures

Value types are defined using the struct keyword instead of class. In most aspects, structures are identical to classes and the characteristics presented in this chapter for classes apply to structures too. However, there are several key differences:

  • Structures do not support inheritance. Although a structure can implement any number of interfaces, it cannot derive from another structure. For this reason, structure members cannot have the protected access modifier. Also, a structured method or property cannot be abstract or virtual.
  • A structure cannot declare a default (parameterless) constructor.
  • Structures can be instantiated without using the new operator.
  • In a structure declaration, fields cannot be initialized unless they are declared const or static.

Enumerations

enum Priority:byte
{
    Low = 10,
    Normal,//11
    Important = 20,
    Urgent//21
}
Priority p = Priority.Normal;
int i = (int)Priority.Normal;
Enum.TryParse("Normal", out Priority p); // p is Normal
Enum.TryParse(typeof(Priority), "normal", true, out object o);
Priority p = (Priority)o;   // p is Normal

Namespaces

namespace chapter_04
{
   namespace demo
   {
      class foo { }
   }
}

//or 

namespace chapter_04.demo
{
   class foo { }
}

There is an implicit namespace that is the root of all namespaces (and contains all namespaces and types that are not declared in a named namespace). This namespace is called global. If you need to include it to specify a fully qualified name, then you must separate it with :: and not with a dot, as in global::System.String. This can be necessary in situations where namespace names collide. Here is an example: