". NET programming Pioneer C #" first chapter C # introduction

Source: Internet
Author: User
Tags error handling exception handling garbage collection inheritance integer new features ole versions
The first chapter of programming C # Introduction welcome you to join the world of C #! This chapter will introduce you to the world of C # and answer related questions such as: Why do you use c#,c++ and C # for the main differences, and why C # makes development easier and makes you feel interesting.
Why is it another programming language?
One question that must be answered: Why learn another language when you are engaged in enterprise development using C + + or VB? The market answer is: "In enterprise Computing, C # will become the main language for writing the next-generation window service (next Generation Windows Services, abbreviated NGWS) application." "This chapter will provide support for parameter requests and exhibit some of the features of C #. This chapter will make you appetizing.
The C # language has evolved from C + +. However, it is modern, simple, fully object-oriented and type-safe. If you are a C/s + + programmer, the learning curve will be very flat. Many C # statements borrow directly from your favorite language, including expressions and operators. If you don't look carefully, you will think of it as C + +.
The most important point about C #: It's a modern programming language. It simplifies and modernize C + + in the fields of class, namespace, method overload, and exception handling. The complexity of C + + is discarded, making it easier to use and less error-prone.
A contribution to the ease of use of C # is the reduction of some of the features of C + +, with no more macros, templates, and multiple inheritance. For enterprise developers in particular, the above features will only create more trouble than benefits.
New features that make programming more convenient are strict type safety, versioning, garbage collection (garbage collect), and so on. All of these features aim to target the development of component-oriented software.

Before I can continue to show more functionality, I want to stop and explain the various elements of C # that are essential below.

Simple
Modern
Object-oriented
Type safety
Version control
Compatible
Flexible


Simple
C # has an advantage that C + + does not have is learning simple. The primary goal of the language is simplicity. Many features (as well as the lack of some of C + + features) Help C # all-round simplicity.
In C #, there is no popular pointer in C + +. By default, you work in managed code where unsafe operations such as direct access to memory are not allowed. I think no C + + programmer can claim to have never used pointers to access memory that does not belong to them.
Closely related to the pointer "drama" is the "stupid" operation. In C + +, there are:: 、.、 and-> operators, which are used in namespaces, members, and references. For beginners, operators are still learning difficulties. C # Discards other operators, using only the single operator ".". Now what a programmer needs to understand is the annotation of a nested name.
You do not have to remember the implied types based on the different processor architectures, or even the range of changes in the various integer types. C # uses a unified type system that discards the changing type system of C + +. This system allows you to view various types as an object, whether it is an original type or a full-blown class. Compared with other programming languages, because of the mechanism of frame (boxing) and extinction frame (unboxing), it is not possible to improve performance by treating simple types as objects. The box and the box will be explained in more detail later, but basically the technique of using object access simple types only when needed.
First, sophisticated programmers may not like it, but integers and booleans are now two completely different data types. This means that the wrong assignment in the original if statement will now be compiled incorrectly because the IF statement only accepts values of Boolean types. There will never be any misuse of the assignment as a comparison character!
C # also solves the surplus (redundancies) that has existed in C + + for some years. This redundancy includes constant predefined, different character types, and so on. Since redundant forms have disappeared from the language, forms are generally available in C #.

Modern
Your effort to learn C # is a big investment, because C # is designed to write the main language for NGWS applications. You'll find many of the features you can implement or work with C + + that are just a few of the basic features in C #.
New types of financial data are popular for enterprise-class programming languages. You have used a new decimal data type that is designed to be used in financial computing. If you don't like this out-of-the-box type, you can easily create a new data type based on your application's specific needs.
As I have mentioned, pointers are no longer part of your programming weapons. Don't be surprised, comprehensive memory management is no longer your task. Runtime NGWS provides a garbage collector responsible for memory management in a C # program. Because memory and applications are managed, it is necessary to enhance type safety to ensure application stability.
For C + + programmers, exception handling is not something new, but it is the main function of C #. C # 's exception handling differs from C + + in that it is a cross language (another feature of the runtime). Before C #, you had to deal with weird HRESULTs, but now it's all over because of the robust error handling based on the exception.
For modern applications, security is paramount, and C # is no exception. It provides a metadata syntax for declaring the capabilities and permissions of the following NGWS security patterns. Metadata is a key concept in the NGWS runtime, and the next chapter will cover its deeper implications.

Object-oriented
You don't expect a new language to support object-oriented features, do you? C # certainly supports all key object-oriented concepts, such as encapsulation, inheritance, and polymorphism. The complete C # class pattern is built at the top of the NGWS runtime's Virtual object system (Vos,virtual object systems), VOS described in the next chapter. Object patterns are only part of the foundation and are no longer part of the programming language.
The first thing you have to focus on is that you no longer have global functions, variables, or constants. Everything is encapsulated in a class, including case members (through instances of the class--objects can be accessed) or all static members (through data types). These make C # code easier to read and help reduce potential naming conflicts.
The methods in the definition class are non-virtual by default (they cannot be overridden by derived classes). The main argument is that this eliminates some other source code errors due to occasional rewrite methods. To override a method, you must have an explicit virtual flag. This behavior not only reduces the speed of the virtual function table, but also ensures the correct version of the control.
Writing classes using C + + allows you to set different access levels for class members using access permissions (access modifiers). C # also supports private, protected, and public three types of access, and adds a fourth: internal. Details about access rights are described in chapter fifth, "Classes."
How many classes you have created are derived from the Doki class (ATL programmers, your votes are counted!). ) ? In most cases, you just derive from a class. Doki often cause more trouble than they solve. That's why C # allows only one base class. If you feel you need multiple inheritance, you can use interfaces.
A possible problem: there is no pointer in C #, how do you imitate it? The answer to this question is representative, and it provides support for the NGWS runtime event pattern. Again, I will put the full explanation of it into chapter fifth.

Type safety
I select the pointer again as an example. With a pointer in C + +, you are free to cast it to any type, including the folly of forcing a int* (integer pointer) to be cast into a double * (double pointer). As long as memory supports this operation, it is "done". This is not the type safety of the Enterprise programming language that you imagine.
The outline of the problem is that C # enforces the strictest type safety to protect itself and the garbage collector (garbage collector). So you have to follow the rules of some related variables in C #:
You cannot use variables that are not initialized. For the object's member variables, the compiler is responsible for zeroing. And local variables, you are responsible for zeroing. When you use a variable that is not initialized, the compiler teaches you what to do. The advantage is that you can avoid errors that result from using uninitialized variables to calculate results, and you don't yet know how these strange results are generated.
C # cancels unsafe type conversions. You cannot cast an integer to a reference type (such as an object), and C # verifies that the conversion is correct when the conversion is down. (that is, the derived class is really derived from the class that is transitioning down.) )
Boundary checks are part of C #. This will never happen again: When an array actually defines only the n-1 element, it uses n elements in excess.
Arithmetic operations can overflow the range of end-value data types. C # allows these operations to be detected at the statement level or at the application level. When an overflow is allowed to be detected, an exception is thrown when an overflow occurs.
In C #, the referenced parameters that are passed are type-safe.

Version controllable (versionable)
In the past few years, almost all programmers have had to deal with the well-known "DLL Hell" at least once. This problem results from multiple applications that have different versions of the same DLL name installed. Sometimes, older versions of an application can work well with new versions of DLLs, but more often they break down. The current version of the problem is really a headache.
As you will see in chapter eighth, "Write components with C #", NGWS runtime will provide version support for the applications you write. C # can best support version control. Although C # does not ensure proper versioning, it is possible for programmers to guarantee versioning. With this support, a developer can ensure that when his class library is upgraded, it retains the binary compatibility of existing client applications.

Compatible
C # does not exist in a closed world. It allows access to different APIs using the most advanced NGWS common language Provisions (Common Language specification, abbreviated as CLS). The CLS provides a standard for the internal operation of a language that conforms to this standard. To enforce the CLS's compilation, the C # compiler detects all public export compilations and lists errors when it is not.
Of course, you also want to be able to access older COM objects. The NGWS runtime provides access to COM transparency. How to integrate the original code will be introduced in chapter 10th, "Internal operations of unmanaged code."
OLE Automation is a special kind of animal. Anyone who creates an OLE Automation project using C + + already has a crush on a variety of automation data types. The good news is that C # supports them without the details of annoying locks.
Finally, C # allows you to use the C prototype API to hold internal operations. You can access the entry point in any DLL from your application (with the prototype of C). The functionality used to access the original API is called the Platform invoke service (Plaform invocation Services, abbreviated Pinovke), and chapter 10th shows some examples of internal operations using the C API.

Flexible
The last paragraph in the previous section may be a reminder to programmers. You might ask, "Don't I have an API to pass pointers to?" You are right. There are not only a few of these APIs, but many (somewhat conservative estimates). This access to raw WIN32 code sometimes results in the use of pointers to unsafe classes (although some of them can be resolved by COM and PInvoke support).
Although the default state of C # code is type-safe, you can declare some classes or simply declare the class's methods to be unsafe types. Such a declaration allows you to statically assign an array using pointers, structs, and so on. Both the security code and the non security Code run in the same management space, implying that when the unsafe code is invoked from the security code, it does not fall into the column set (marshaling).

Summary
The C # language evolves from C and C + +, and is created for enterprise developers who are willing to sacrifice C + + for a bit of the underlying functionality to get more convenient and more product. C # Modern, simple, object-oriented, and type-safe. Although it borrows a lot from C and C + +, there are huge differences in certain areas such as namespaces, classes, methods, and exception handling.
C # provides you with convenient features such as garbage collection, type safety, versioning, and so on. The only "price" is that code actions are type-safe by default and do not allow pointers. The type safety alone can be done. However, if you need pointers, you can still use them through unsafe code, and you cannot include a column set when you call a non-security code.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.