C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

Namespaces/Identifiers

Namespaces

You should not call classes with the using keyword. Hence, the code in Listing 1.4.2 results in a compilation error because Console is a class:

Listing 1.4.2

// compilation error
using System.Console;

Because the term Console is one of the classes located in the System namespace, you apply it along with its built-in methods. For example, you can write text to the command prompt by using the WriteLine() method of the Console class, as shown in Listing 1.4.3.

Listing 1.4.3

Console.WriteLine("Hello World");

Even though you cannot directly apply the class names along with the using directive, you can create an alias, as shown in Listing 1.4.4:

Listing 1.4.4

using mydotnet = System.Console;

After that, you have to apply the alias name, mydotnet, in your C# program, as shown in Listing 1.4.5:

Listing 1.4.5

mydotnet.WriteLine("Hello C#");

You can, however, completely omit the namespace declaration (with the using keyword) in a C# program. But, as an alternative, you can refer to the namespace name in the beginning of the relevant line of each statement, as shown in Listing 1.4.6:

Listing 1.4.6

System.Console.WriteLine("Welcome to C#");

A list of .NET namespaces is shown in the following table:

System.Collections System.IO
System.Data System.Net
System.Data.OleDb System.Reflection
Stsrem.Data.SqlClient System.Runtime.InteropServices
System.Data.OracleClient System.Runtime.Remoting
System.Diagnostics System.Security
System.Drawing System.Threading
System.Drawing.Drawing2D      System.Web
System.Drawing.Printing System.Xml
System.Windows.Forms

-------------------------------------------------------------------------
 

---------------------------------------------------------------------------
Assembly

An assembly is a file that is automatically generated by the compiler upon successful compilation of every .NET application. It can be either a Dynamic Link Library or an executable file. It is generated only once for an application and upon each subsequent compilation the assembly gets updated. The entire process will run in the background of your application; there is no need for you to learn deeply about assemblies. However, a basic knowledge about this topic will help you to understand the architecture behind a .NET application.

 

An Assembly contains Intermediate Language (IL) code, which is similar to Java byte code. In the .NET language, it consists of metadata. Metadata enumerates the features of every "type" inside the assembly or the binary. In addition to metadata, assemblies also have a special file called Manifest. It contains information about the current version of the assembly and other related information.

In .NET, there are two kinds of assemblies, such as Single file and Multi file. A single file assembly contains all the required information (IL, Metadata, and Manifest) in a single package. The majority of assemblies in .NET are made up of single file assemblies. Multi file assemblies are composed of numerous .NET binaries or modules and are generated for larger applications. One of the assemblies will contain a manifest and others will have IL and Metadata instructions.
-----------------------------------------------------------------------------------------------------------------------------------------------------

 

Identifiers

Identifiers are names given to namespaces, classes, methods, variables, and interfaces. An identifier must be a whole word and start with either a letter or an underscore. It can be any combination of letters, numbers, and underscores. But, it should not start with a number. Prior to .NET, programmers were using Hungarian-notation guidelines for naming identifiers; however, with .NET Microsoft has recommended Pascal and Camel notations for identifiers. They have also suggested using semantics in the identifier name. Another point you should bear in mind is that identifiers should not be the same as a C# keyword as listed in the section "Keywords." For example, the following code is illegal:

// Error. int cannot be used as a variable name as it a keyword
int int = 5;
 

Identifiers in C# are case sensitive. For instance, X is not equal to x. Some programmers use the @ prefix as a first character when declaring identifiers to avoid a clash with a keyword, but it is not a recommended practice. The following names are valid identifiers in C#:

  1. Hello
  2. hello
  3. H_ello
  4. HelloWorld
  5. X
  6. x
Note: In C#, class names can be different from file names.

You should name the variables using the standard DataType prefixes. Also, the first letter after the prefix should be capitalized. Table 1.6.1 shows a list of prefixes for the various .NET DataTypes. You will learn more about DataTypes in Part 3 of C#—Learning with the FAQs.

Table 1.6.1 List of C# Data Types

Data TypePrefixExample
ArrayarrarrNumber
BooleanblnblnSelect
BytebytbytNumber
CharchrchrPick
DateTimedtmdtmPick
DecimaldecdecPoint
DoubledbldblData
IntegerintintVar
LonglnglngMiles
ObjectobjobjVar
ShortshrshrNumber
SinglesngsngNumber
StringstrstrAddress

Interfaces are usually named with an "I" as the first letter. All Windows Forms controls should be named with the special prefixes, as shown in Table 1.6.2. This is to avoid confusion and also to distinguish among other controls in a complex project. As explained above, the first letter after the prefix should be capitalized. Once you master the naming conventions and prefixes, it will be very easy for you to write and debug the code at a later stage.

Table 1.6.2 List of prefixes for Windows Forms controls

Control NamePrefixExample
ButtonbtnbtnSubmit
TextBoxtxttxtFname
CheckBoxchkchkHobbies
RadioButtonradradMale
ImageimgimgIndia
LabellbllblCity
CalendarcalcalDate

It is beyond the scope of this FAQ to cover the prefixes of all the .NET controls. You will find a detailed list of them in the MSDN Library. (Perform a search using the phrase "naming guidelines")

posted on 2005-11-18 12:49 梦在天涯 阅读(403) 评论(0)  编辑 收藏 引用 所属分类: C#/.NET


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1783931
  • 排名 - 5

最新评论

阅读排行榜