C# – Using Namespaces

Namespaces

Namespaces are domain type names which makes it easier to find files and also it avoid name conflicts. They are independent of assemblies, which are units of deployment such as an .exe or .dll file.

The namespace keyword defines a namespace for types within that block
Example:

namespace Outer.Middle.Inner
{
class Class1 {}
class Class2 {}
}

the dots in the namespace indicate a hierarchy of nested namespace.

using Directive

The using directive imports a namespace.

using static (C#6)

Can import specific type with using static directive and all static members of that type can be used.

using static System.Console;

class Test
{
   static void Main() 
   {
      WriteLine ("Hello");
   }
}

The using static directive imports all accessible static members of the type, including fields, properties and nested types

You can also apply this directive to enum types using static System.Windows.Visibility;

var textBox = new TextBox { Visibility = Hidden ;}

Rules Within a Namespace

  1. Name scoping
    Names declared in outer namespace can be used unqualified within inner namespace. If you want to refer to a type in a different branch of your namespace hierarchy, you can use a partially qualified name.
  2. Name hiding
    If the same type name appears in both an inner and an outer namespace, the inner name wins.
  3. Repeated namespaces
    You can repeat a namespace declaration, as long as the type names within the namespaces don’t conflict
  4. Nested using directive
    You can nest a using directive within a namespace. This allows you to scope the using directive within a namespace declaration.
  5. Aliasing Types and Namespaces
    You can import just the specific types you need, giving each type an alias

Advanced Namespace Features

  1. Extern
    Extern aliases allow your program to reference two types with the same fully qualified name
  2. Namespace alias qualifiers
    Names in inner namespaces hide names in outer namespaces.

C# – Stack-overflow Exception

Stack-overflow Exception

The exception is triggered because of too many nested method calls. Each method call is kept on the stack memory. The program terminates because of infinite and uncontrolled recursion. Process terminated message is displayed with no recovery option also try-catch block will not be able to catch the exception.

One of the common place the exception happens is when setting the get and set methods in a class and forgetting to set the backing field. This happens because you are recursively calling the property again which creates infinite recursion at run time. This class will compile successfully but will throw exception during run time.
Example:

public class Data
  {
        public string MemberId { 
            get {return this._ID;}
            set { this._ID = value;}
        } 

        public string ALT_MEM { get; set; } 
  }

To solve this you will need to declare private backing field to hold the value.

Example:

public class Data
  {
        //You need to declare this to avoid stackoverflow exception
        private string _ID;

        public string MemberId { 
            get {return this._ID;}
            set { this._ID = value;}
        } 

        public string ALT_MEMBNO { get; set; } 


  }

If you are using the auto-property feature, it will create a hidden backing field so you don’t need to worry about it.

  
public string FirstName { get; set;}

Live as if you were to die tomorrow. Learn as if you were to live forever..
-Mahatma Gandhi