<Presenting C #> Chapter 1 C # Introduction (Revised)

zhaozj2021-02-08  383

Chapter 1 C # Introduction

Welcome to the world of C # (read C Sharp)! This chapter will let you know C # and answer some related questions, such as: Why do you have any differences in C #, C and C #, and why C # makes software development easier and more interesting.

1.1 Why is another programming language?

You must answer one question: When you have used C or VB engaged in enterprise development, do you have to learn another language? Answer this question with business's point of view: "In the field of corporate calculations, C # will become used to write The primary language of the new generation Windows Services' (Micws) application is the main language of the application. "This chapter will support the parameter request and display some of the features of C #, which will make your appetite .

C # language is evolved from C / C . However, it is modern, simple, fully objective and type safety. If you are a C / C programmer, the learning curve will be flat. Many C # statements have borrowed the language you love, including expressions and operators. At first glance, I thought it was C .

About C # most important point: it is a modern programming language. It simplifies and innovates the fields of classes, namespaces, method overloads, and abnormalities in C . Abandon C complexity, making it more easy to use, less error.

C # reduces some of the functions of C , it is easier to use. No more macro, template and multiple inheritance. Especially for corporate developers, the above functions will only have more trouble rather than benefits.

New features that make programming are strict types of security, version control, garbage collection (Garbage Collect). The goals of all these features are to develop components-oriented software.

Before continuing to present more features, I want to explain the various elements of the C #.

· Simple

·modern

· Object-oriented

· Type security

·version control

·compatible

·flexible

1.1.1 simple

C # has a advantage that C is not easy to learn. The primary goal of the language is simple. Many functions (it is better to miss some of the features of C ) helps C # all simplify.

In C #, there is no popular pointer in C . By default, you work in a managed code, where you do not allow unsafe operations such as direct access to memory. I think no C programmer can claim that they have never used pointer access to memory that does not belong to them.

Closely related to the "tragic" of the pointer is "stupid" operation. In C , there are "::", ".", And "->" operators, they are used for namespace, members, and references, respectively. For novices, operators are still a difficult relationship between learning. C # Discard other operators, only single operators "." Now that the programmer needs to understand the nested name.

You don't have to remember the implicit type of different processor architecture, even a variety of changes in various intellectuals. C # uses a unified type system to abandon C multi-change type system. This system allows you to view various types as an object to determine that it is a primitive type or a real class. Compared to other programming languages, simply type as object processing is not allowed to achieve performance. This technology will be explained in detail later, but basically, simply type as an object can be accessed only when needed.

First of all, the older programmer may not like it, but integer and Boolean are now two completely different data types. This means that the incorrect assignment in the original if statement will now be compiled, because the IF statement only accepts the value of the Boolean type. Never have misuse assault characters as a comparison! C # simultaneously solves redundancy in some years in C . This redundancy includes a #define predefined for constants and a variety of different character types. In view of the redundant format has disappeared from the language, these formats can be used in C #.

1.1.2 Modern

Your effort you put into learning C # is a big investment because C # is designed as a main language written by NGWS applications. You will find a lot of functions that you can implement or costly with C , just part of the basic features in C #.

For enterprise programming languages, new financial data types are very popular. You have used a new decimal data type, which is dedicated to financial computing. If you don't like this ready-made simple type, you can easily create a new data type according to the special needs of the application.

I have mentioned that the pointer is no longer part of your programming weapon. Don't be too surprised, comprehensive memory management is no longer your task. Running NGWS provides a garbage collector that is responsible for memory management in the C # program. It is necessary to enhance type security due to memory and applications, so it is necessary to secure the type of security to ensure the stability of the application.

For C programmers, exception processing is not new, but it is the main function of C #. C # abnormal processing and C difference lies in it is a cross-language platform (another function at runtime). Before you don't have C #, you must handle weird HRESULTS, but now it's ending because of an abnormal robust error processing.

For modern applications, security is the primary, C # will not excera way. It provides metadata syntax for declaring the performance and licenses of the following NGWS security mode. Metadata is a key concept of NGWS Runtime, which will involve it deeper meaning.

1.1.3 Object-Orientation

You will not expect a new language that does not support object-oriented features? C # Of course, support all key object-oriented concepts such as encapsulation, inheritance, and polymorphism. The complete C # class mode is built on the upper layer of the Virtual Object System (VOS, Virtual Object System) of the NGWS Runtime, and the VOS will be described in the following seal. The object mode is only the foundation, and it is no longer part of the programming language.

If you have to pay attention, there is no longer global function, variable or constant. All things are encapsulated in the class, including instance members (accessible through class instance-objects, access to members) or static members (accessible via data type). This makes the C # code more easy to read and help reduce potential naming conflicts.

The method in the definition class is default and non-virtual (they cannot be rewritten by derived classes). The main argument is that this will eliminate some other original code errors due to occasional reform methods. To rewrite methods, you must have a clear virtual mark. This behavior not only reduces the size of the virtual function table, but also ensures the correct version of the control.

Use C to write classes, you can use access modifiers to set different access levels for class members. C # also supports three access rights of Private, Protected, and Public, and also adds fourth: internal. Details of the depot decoration will be described in Chapter 5, "Class".

How many classes you have created are derived from multiple base classes (ATL programmers, your voting is not just!)? In most cases, as long as it is derived from a class. The troubles that have a multi-base class is usually more than their solutions. This is why C # only allows a base class. If you feel that you need multiple inheritance, you can use the interface.

One possible problem: there is no pointer in C #, how to imitate it? This question is very representative, it provides support for NGWS Runtime event mode. Again, I will put a comprehensive explanation of it to the fifth chapter. 1.1.4 Type Safety

I will choose the pointer again as an example. Have a pointer in C , you can freely convert it into any type, including stupid things such as putting an int * (integer pointer) into a Double * (double precision pointer). As soon as the memory supports this operation, it works. This is not the type of business-level programming language you imagined.

Principled issues, C # implement the most stringent types to protect themselves and garbage collectors. Therefore, some rules for some of the variables in C # must be observed:

You can't use a variable without initialization. For member variables of the object, the compiler is responsible for clearing. And local variables are responsible for clearing. When you use a variable that is not initialized, the compiler will teach you how to do it. The advantage is to avoid errors caused by the use of variables that are not initialized, and you still don't know how these strange results are generated.

C # cancels unsafe type conversion. You cannot convert an integer to a reference type (such as an object), and when it is converted down, C # verifies that this conversion is correct. (That is, the derived class is really born from the class that is converted down.)

Boundary check is part of C #. There will never happen: When the array actually defines N-1 elements, n elements are overprint.

The arithmetic operation is likely to overflow the range of the result data type. C # allows these operations to be detected in statement levels or application levels. In the case where the overflow is allowed, an exception will be triggered when the overflow occurs.

In C #, the reference parameters transmitted are type security.

1.1.5 Versionable

Over the past few years, almost all programmers have had at least once to relate to well-known "DLL Hell". This problem has different versions of the same DLL name due to multiple applications. Sometimes, the old version of the app works well with the new version of the DLL, but when they interrupt the run. The current version is really a headache.

As you will be seen in Chapter 8, "Write Components" with C # Write components, NGWS Runtime will provide version support for the application. C # can best support version control. Although C # does not ensure the correct version control, it can guarantee version control for programmers. With this support, a developer ensures that when his class library is upgraded, it still retains binary compatibility of the existing customer application.

1.1.6 compatible

C # does not exist in a closed world. It allows different APIs to access different APIs using the most advanced NGWS General Language Specification (Common Language Specification, Simmon Language Specification). CLS specifies a standard that makes a language that meets this standard can work together. In order to enhance the compilation of CLS, the C # compiler detects all public exit compilation and lists errors when passing.

Of course, you also want to access the old COM object. NGWS Runtime provides access to COM transparent. How to integrate the original code will be introduced in Chapter 10 "Collaborative Work of Non-managed Codes".

OLE automation is a special animal. Anyone who uses C creates an OLE automation project already likes a variety of automated data types. There is a good news is that C # supports them, and there is no detail involving troubles.

Finally, C # allows you to collaborate with the C prototype API. You can access the entry point in any DLL from your application. The feature used to access the original API is called the Platform Call Service (PLAFORM Invocation Services, abbreviations, Chapter 10), will illustrate some examples of collaborative work using the C API.

1.1.7 flexible

The last section of the previous section is likely to remind the programmer. You may ask: "Don't you do not send the API of the pointer?" It is correct, not only a few of this API, but there are many (a little conservative estimate). This access to the original Win32 code sometimes causes the use of non-secure class specific pointers (although some of them can be solved due to support from COM and PINVOKE). Although the default state of C # code is type secure, you can declare that some categories or methods of declaring classes are non-secure types. Such a statement allows you to use pointers, structures, and static allocation arrays. The security code and non-security code run in the same management space, which suggests that it will not fall into the column (Marshaling) when calling a non-security code from the security code.

1.2 small knot

C # language evolve from C and C , it is created for companies that are willing to sacrifice C a bit function to get more convenient and more productive business developers. C # modern, simple, object-oriented and types of security. Although it draws on many things of C and C , there is still a huge difference between some specific areas such as name space, classes, methods, and abnormalities.

C # provides you with convenient features, such as garbage collection, type security, version control, and more. Only "cost" is that the code operation is secure, and the pointer is not allowed. However, most issues can be solved in the type of light. However, if you need a pointer, you can still use them by non-security code, and when calling a non-security code, you cannot contain mashalls.

转载请注明原文地址:https://www.9cbs.com/read-790.html

New Post(0)