命名空间????? [英] Namespaces?????

查看:81
本文介绍了命名空间?????的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑下面的代码来自MSDN。


为什么命名空间中的using语句? 


问候


Manoj

 //一组用于处理书店的类:
namespace Bookstore
{
使用System.Collections;

//描述书籍清单中的书籍:
public struct Book
{
public string Title; //这本书的标题。
public string作者; //本书的作者。
公共小数价格; //这本书的价格。
public bool平装本; //是平装本吗?

public Book(字符串标题,字符串作者,十进制价格,bool paperBack)
{
Title = title;
作者=作者;
价格=价格;
平装书= paperBack;
}
}

//声明处理书籍的委托类型:
public delegate void ProcessBookDelegate(Book book);

//维护图书数据库。
公共类BookDB
{
//数据库中所有书籍的列表:
ArrayList list = new ArrayList();

//向数据库添加一本书:
public void AddBook(字符串标题,字符串作者,十进制价格,bool paperBack)
{
list.Add(新书(标题,作者,价格,paperBack));
}

//在每本平装书上调用传入的委托来处理它:
public void ProcessPaperbackBooks(ProcessBookDelegate processBook)
{
foreach (列表中的b)
{
if(b.Paperback)
//调用委托:
processBook(b);
}
}
}
}


//使用Bookstore类:
命名空间BookTestClient
{使用书店
;

//账簿总价和平均价格:
class PriceTotaller
{
int countBooks = 0;
十进制priceBooks = 0.0m;

internal void AddBookToTotal(Book book)
{
countBooks + = 1;
priceBooks + = book.Price;
}

内部小数AveragePrice()
{
return priceBooks / countBooks;
}
}

//测试图书数据库的类:
class TestBookDB
{
//打印书的标题。
static void PrintTitle(Book b)
{
System.Console.WriteLine(" {0}",b.Title);
}

//执行从这里开始。
static void Main()
{
BookDB bookDB = new BookDB();

//使用一些书籍初始化数据库:
AddBooks(bookDB);

//打印所有平装书的标题:
System.Console.WriteLine(&"Paperback Book Titles:");

//创建一个与静态
//方法相关联的新委托对象Test.PrintTitle:
bookDB.ProcessPaperbackBooks(PrintTitle);

//使用
获得平装书的平均价格//一个PriceTotaller对象:
PriceTotaller totaller = new PriceTotaller();

//在对象totaller上创建一个与非静态
//方法AddBookToTotal相关联的新委托对象:
bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);

System.Console.WriteLine(" Average Paperback Book Price:$ {0:#。##}",
totaller.AveragePrice());
}

//使用一些测试书初始化book数据库:
static void AddBooks(BookDB bookDB)
{
bookDB.AddBook(" C Programming Language","Brian W. Kernighan and Dennis M. Ritchie",19.95m,true);
bookDB.AddBook(" The Unicode Standard 2.0",The Unicode Consortium",39.95m,true);
bookDB.AddBook(" The MS-DOS Encyclopedia"," Ray Duncan",129.95m,false);
bookDB.AddBook(" Dogbert's Clues for the Clueless"," Scott Adams",12.00m,true);
}
}
}
/ *输出:
平装书标题:
C编程语言
Unicode标准2.0
Dogbert's Cluees for the Clueless
平均平装书价格:$ 23.97
* /



解决方案

我们也是故意使用的。  阅读此博客文章,其中说明何时使用
在命名空间内部或外部使用


https://blogs.msdn.microsoft.com/ericlippert/2007/06/25/inside-or-outside/


Please consider the code below it is from MSDN.

Why is the using statement within the namespace? 

regards

Manoj

// A set of classes for handling a bookstore:
namespace Bookstore
{
    using System.Collections;

    // Describes a book in the book list:
    public struct Book
    {
        public string Title;        // Title of the book.
        public string Author;       // Author of the book.
        public decimal Price;       // Price of the book.
        public bool Paperback;      // Is it paperback?

        public Book(string title, string author, decimal price, bool paperBack)
        {
            Title = title;
            Author = author;
            Price = price;
            Paperback = paperBack;
        }
    }

    // Declare a delegate type for processing a book:
    public delegate void ProcessBookDelegate(Book book);

    // Maintains a book database.
    public class BookDB
    {
        // List of all books in the database:
        ArrayList list = new ArrayList();

        // Add a book to the database:
        public void AddBook(string title, string author, decimal price, bool paperBack)
        {
            list.Add(new Book(title, author, price, paperBack));
        }

        // Call a passed-in delegate on each paperback book to process it: 
        public void ProcessPaperbackBooks(ProcessBookDelegate processBook)
        {
            foreach (Book b in list)
            {
                if (b.Paperback)
                    // Calling the delegate:
                    processBook(b);
            }
        }
    }
}


// Using the Bookstore classes:
namespace BookTestClient
{
    using Bookstore;

    // Class to total and average prices of books:
    class PriceTotaller
    {
        int countBooks = 0;
        decimal priceBooks = 0.0m;

        internal void AddBookToTotal(Book book)
        {
            countBooks += 1;
            priceBooks += book.Price;
        }

        internal decimal AveragePrice()
        {
            return priceBooks / countBooks;
        }
    }

    // Class to test the book database:
    class TestBookDB
    {
        // Print the title of the book.
        static void PrintTitle(Book b)
        {
            System.Console.WriteLine("   {0}", b.Title);
        }

        // Execution starts here.
        static void Main()
        {
            BookDB bookDB = new BookDB();

            // Initialize the database with some books:
            AddBooks(bookDB);

            // Print all the titles of paperbacks:
            System.Console.WriteLine("Paperback Book Titles:");

            // Create a new delegate object associated with the static 
            // method Test.PrintTitle:
            bookDB.ProcessPaperbackBooks(PrintTitle);

            // Get the average price of a paperback by using
            // a PriceTotaller object:
            PriceTotaller totaller = new PriceTotaller();

            // Create a new delegate object associated with the nonstatic 
            // method AddBookToTotal on the object totaller:
            bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);

            System.Console.WriteLine("Average Paperback Book Price: ${0:#.##}",
                    totaller.AveragePrice());
        }

        // Initialize the book database with some test books:
        static void AddBooks(BookDB bookDB)
        {
            bookDB.AddBook("The C Programming Language", "Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true);
            bookDB.AddBook("The Unicode Standard 2.0", "The Unicode Consortium", 39.95m, true);
            bookDB.AddBook("The MS-DOS Encyclopedia", "Ray Duncan", 129.95m, false);
            bookDB.AddBook("Dogbert's Clues for the Clueless", "Scott Adams", 12.00m, true);
        }
    }
}
/* Output:
Paperback Book Titles:
   The C Programming Language
   The Unicode Standard 2.0
   Dogbert's Clues for the Clueless
Average Paperback Book Price: $23.97
*/

解决方案

We use likewise on purpose. Read this blog post which explains when to use Using inside or outside namespace.

https://blogs.msdn.microsoft.com/ericlippert/2007/06/25/inside-or-outside/


这篇关于命名空间?????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆