Csharp/CSharp Tutorial/Language Basics/Namespace Alias — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:19, 26 мая 2010

Aliasing Class Names

using Class1 = Namespace1.TestClass;
using Class2 = Namespace2.TestClass;
   
namespace Namespace1
{
    class TestClass
    {
        public TestClass()
        {
            System.Console.WriteLine("Hello from Namespace1.TestClass!");
        }
    }
}
   
namespace Namespace2
{
    class TestClass
    {
        public TestClass()
        {
            System.Console.WriteLine("Hello from Namespace2.TestClass!");
        }
    }
}
   
class MainClass
{
    public static void Main()
    {
        Class1 Object1 = new Class1();
        Class2 Object2 = new Class2();
    }
}

Aliasing Namespace Names

using N1 = Namespace1;
using N2 = Namespace2;
   
namespace Namespace1
{
    class TestClass
    {
        public TestClass()
        {
            System.Console.WriteLine("Hello from Namespace1.TestClass!");
        }
    }
}
   
namespace Namespace2
{
    class TestClass
    {
        public TestClass()
        {
            System.Console.WriteLine("Hello from Namespace2.TestClass!");
        }
    }
}
   
class MainClass
{
    public static void Main()
    {
        N1.TestClass Object1 = new N1.TestClass();
        N2.TestClass Object2 = new N2.TestClass();
    }
}

Global Namespace Alias

using System;
using System.Collections.Generic;
using System.Text;
using System.ruponentModel;
class Configuration {}
namespace My
{
    class Configuration {}
    class GlobalNamespaceAlias
    {
        static void Main()
        {
            Console.WriteLine(typeof(Configuration));
            Console.WriteLine(typeof(global::Configuration));
            Console.WriteLine(typeof(global::My.GlobalNamespaceAlias));
        }
    }
}

Simple Aliases for namespace

using System;
using WinForms = System.Windows.Forms;
using WebForms = System.Web.UI.WebControls;
using System.ruponentModel;
    class SimpleAliases
    {        
        static void Main()
        {
            Console.WriteLine (typeof (WinForms.Button));
            Console.WriteLine (typeof (WebForms.Button));
        }
    }

Use the global alias.

using System;  
  
using Ctr = Counter;  
 
namespace Counter {  
  class MyClass {  
  }  
}  
  
// Declare another class called CountDown, which  
// is in the global namespace.  
class MyClass { 
} 
 
class GlobalAliasQualifierDemo {  
  public static void Main() {  
 
    Ctr::MyClass m = new Ctr::MyClass();  
 
    // Next, create CountDown object from global namespace. 
    global::MyClass m2 = new global::MyClass();  
  }  
}

Using a Namespace Directive

using System;
   
using Class1 = Namespace1.TestClass;
using Class2 = Namespace2.TestClass;
   
namespace Namespace1
{
    class TestClass
    {
        public TestClass()
        {
            Console.WriteLine("Hello from Namespace1.TestClass!");
        }
    }
}
   
namespace Namespace2
{
    class TestClass
    {
        public TestClass()
        {
            Console.WriteLine("Hello from Namespace2.TestClass!");
        }
    }
}
   
class MainClass
{
    public static void Main()
    {
        Class1 Object1 = new Class1();
        Class2 Object2 = new Class2();
    }
}

Using the alias keyword to refer to a nested namespace.

using CmpDb = Company.InternalClasses.Database.Table;
namespace Company.InternalClasses {
    public class DBInterface {
        public static void Open() {
            System.Console.WriteLine("Opening database");
        }
    }
    // Define a nested namespace within InternalClasses
    namespace Database {
        public class Table {
            public static void Open(string tblName) {
                System.Console.WriteLine("Opening table {0}", tblName);
            }
        }
    }
}
public class MainClass {
    public static void Main() {
        CmpDb.Open("fred");
    }
}