Inside C#_Chapter1_3

 

“Hello, World” Code Walk-Through

Hello World 代码演练

Congratulations! While we haven’t exactly launched the space shuttle with our little application, you have written your first C# application. Next let’s talk a little about the code itself and learn some basic C# syntax and concepts.

恭喜您!虽然我们还不能用这个小应用程序正确的发射航天飞机,但你已经完成了你的第一个C#应用程序。(谢谢zleo的建议!)下面我们讲解一点关于代码的细节和C#的基本语法和概念。

One-Stop Programming

一站式编程

The first thing to realize when coding in C# is that when you define a class and its methods, you actually define the methods within the class body itself. If you’re background is in C++ programming, you’re accustomed to doing this for very small function bodies. However, in the case of C#, all methods are defined within the class definition. The reason for this is simple. The C# language is designed to give you the ability to create “mobile” code.

首先我们应该明白当你在C#中编写代码定义一个类或他们的方法时,你实际是在类的内部定义他们自身的方法。如果你有C++的编程经验,你可能习惯用一些小函数来实现他们。然而在C#中,所有的方法都是在类定义的内部定义的。因为这样做更简单。C#被设计为可以为你提供设计“可移动”代码的能力。

In other words, when you write a C# class, you end up with a fully encapsulated bundle of functionality that you can easily drop into any other development environment—without worrying how that language processes include files or whether it has a mechanism for including files within files. Using this one-stop programming approach, you can, for instance, take an entire class and drop it into an Active Server Pages (ASP) page. The class will then function as though you were compiling it into a desktop Windows application!

换句话说,当你在C#中编写类,最终你将会得到一个完全封装的功能包,你可以非常容易的把它放到任何其它的开发环境中去,而不用担心他们用社么样的语言处理包含的文件,以及是否有一个将包含文件嵌入文件的机制。使用一站式编程方法,你能够实现获得一个类并将它放置到一个ASP(Active Server Page)页面中,类将能够像被编译到一个Windows桌面应用程序中一样的被操作。

Namespaces

命名空间

The first line of code that you see in the demo is the definition of a namespace named InsidCSharp. For those who are new to namespaces, they are simply a convenient means of semantically grouping elements—such as classes and other namespaces—to avoid name collision. Theoretically, you can nest namespaces to any desired level.

示例代码中的第一行是定义一个名为InsideCSharp的命名空间,命名空间是一种对元素(例如象类和其他的命名空间)按语义分组的简便快捷的手段以避免名称冲突。理论上说,你可以无限制的实现命名空间的嵌套。

For example, you might want to use namespaces when you know your code will be used in an environment in which the names of your classes might duplicate other classes. Let’s say you’re writing a grid component and want to either sell it or release it to the general public. You could prepend all your class names with your company name or some special prefix, but with namespaces, you could simply define your classes as follows:

例如,当你的代码将被用于一个环境中而且你的类的命名可能会和环境中其它的类冲突你就需要使用命名空间。我们假定你编写一个grid组件并且希望销售它或公开的发行它。你也许得预先考虑使用你公司的名称或一些特别的前缀来命名你的所有类,但是如果使用命名空间,你可以象下面代码所示的简单的定义你的类:

namespace MyCompany

{

    class Grid

    {

    }

 

    class OtherClass

    {

    }

}

Now when the user of your grid wants to reference it, his or her code would simply fully qualify the class name with the namespace name—MyCompany.Grid.

现在当你的用户去引用Grid类时,他们可以使用命名空间完全限定类名来简单的编码:MyCompany.Grid

Namespace declarations consist of the keyword namespace followed by the namespace name and a body—and optionally terminated with a semicolon. Here’s the syntax for defining a namespace in C#:

命名空间用namespace关键字后跟命名空间名和命名空间体(多项之间用“;”隔开)来声明,下面是C#中声明命名空间的语法:

namespace <namespace_name> 

    <namespace-body> 

}

As mentioned, you can nest namespaces, as in the following example:

如前所述,你可以下下面代码所示嵌套命名空间:

namespace Distrubution

{

    namespace Purchasing

    {

        // Define purchasing classes

    }

 

    namespace Receiving

    {

        // Define receiving classes

    }

 

    namespace Inventory

    {

        // Define inventory classes

    }

}

An alternative means of defining nested namespaces is to fully qualify each namespace. Besides having to do less typing, you’ll benefit from employing this syntax only if you’re defining these namespaces across multiple physical files to be compiled into one application:

作为一个嵌套定义命名空间的方法,你可以使用完全限定符定义每一个命名空间。除了可以减少键盘输入外,当你希望在多个被编译到一个应用程序的物理文件中跨文件定义这些命名空间时(即在多个文件中定义不同的从属命名空间)你可以从这一语法中获得益处:

namespace Distrubution.Purchasing

{

    // Define purchasing classes

}

 

namespace Distrubution.Receiving

{

    // Define receiving classes

}

 

namespace Distrubution.Inventory

{

    // Define inventory classes

}

Although we’ll examine the .NET type system and Framework SDK in Chapter 2, “The .NET Type System,” it’s worth pointing out here that the entire Framework SDK is represented by classes and types that are grouped into namespaces.

虽然我们将在第2章“.Net类型系统”中解释.NET类型系统和Framework SDK。但在这里指出整个Framework SDK都是以被组织到命名空间中的类和类型来呈现的也是非常有意义的。

C# Comments

C#注释

As with C++ and Java, C# supports two types of code comments (textual notes that serve as documentation for source code). In one type the comment is bracketed with /* and */ characters as in the following:

C++Java一样,C#支持两种类型的代码注释(原代码中的原文注解)。一种注释类型使用成对的“/*”和“*/”括起来的多个字符,如下面代码所示:

/* This is a comment */

class DerivedClass : /* I can place a comment in the middle

 of code */ BaseClass

{

    /* I can preceded code with comments */ int MyField;

   …… ……

}

In Chapter 15, “Documentation with XML,” you’ll learn a new way to format your comments that enables you to harness the power of XML and XSL to automatically generate HTML documentation from your C# comments.

在第15章“利用XML编制文档”中,你将学习一种新的利用XMLXSL的强大功能自动的从你的C#代码的注释生成格式化的HTML文档的方法。

 

posted on 2004-11-25 01:10  Dragoon  阅读(1336)  评论(3)    收藏  举报

导航