Chapter C # Introduction to. net Programming pioneer C #

Source: Internet
Author: User

Chapter 1 Introduction to C # Welcome to the world of C! This chapter introduces you to the world of C # And answers some related questions, for example, why do you have to use C #, C ++, and C, and why C # makes development easier and interesting.
Why is it another programming language?
One question that must be answered: When you have used C ++ or VB for enterprise development, why should you learn another language? The market answer is: "in the enterprise computing field, C # will become the main language for compiling" Next Generation Windows Services "(NGWS) applications. "This Chapter provides support for parameter requests and displays some functions of C. This chapter will make you appetizing.
C # Language evolved from C/C ++. However, it is modern, simple, fully object-oriented, and type-safe. If you are a C/C ++ programmer, the learning curve will be smooth. Many C # statements use your favorite language, including expressions and operators. If you do not look at it carefully, you will simply regard it as C ++.
The most important thing about C # is that it is a modern programming language. It simplifies and modernized C ++ in fields such as class, namespace, method overload, and exception handling. The screen discards the complexity of C ++, making it easier to use and less error-prone.
Which contributes to the ease of use of C # is to reduce some features of C ++, without macros, templates, and multi-inheritance. Especially for enterprise developers, the above functions will only produce more troubles than benefits.
New features that make programming more convenient are strict type security, version control, and garbage collect. All these functions aim at developing component-oriented software.

Before continuing to present more features, I would like to stop and explain the various essential elements of C # below.

Simple
Modern
Object-oriented
Type Security
Version Control
Compatible
Flexible


Simple
C # One advantage that C ++ does not possess is that it is easy to learn. The primary goal of this language is simplicity. Many functions (not to mention the lack of some functions of C ++) Help C # complete simplicity.
In C #, no pointers are popular in C ++. By default, you work in managed code, where insecure operations such as direct access to memory are not allowed. I think no C ++ programmer can claim that they have never used pointers to access their memory.
What is closely related to the pointer "dramatic" is the "stupid" operation. In C ++, operators such as:,., and-> are used for namespaces, members, and references. For beginners, operators are still difficult to learn. C # discard other operators and only use a single operator ".". What a programmer needs to understand now is the annotation of nested names.
You do not have to remember the implicit types based on different processor architectures, or even the ranges of various integer types. C # using a unified type system, the C ++ variable type system is discarded. 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, the mechanism of boxing and unboxing does not improve the performance of processing simple types as objects. I will explain in detail box adding and box removing later, but basically only use the simple type of object access technology when needed.
First, sophisticated programmers may not like it, but integer and Boolean types are now two completely different data types. This means that the wrong value assignment in the original if statement will be compiled incorrectly, because the if statement only accepts boolean values. There will no longer be errors such as misuse of the value assignment operator as a comparison character!
C # also solves the redundant things that have existed in C ++ for some years (redundancies ). This type of redundancy includes constant pre-Definition and different character types. Since redundant forms have been removed from the language, forms can be used in C.

Modern
Your effort to study C # is a big investment, because C # is designed to write the main language of the NGWS application. You will find that many functions that can be implemented or implemented with C ++ are just some basic functions in C.
For enterprise-level programming languages, new financial data types are very popular. You use a new decimal data type, which is used for financial computing. If you do not like this easy-to-use type, you can easily create a new data type based on your application's special needs.
As I have mentioned, pointers are no longer part of your programming weapon. Don't be surprised. Full memory management is no longer your task. At runtime, NGWS provides a garbage collector responsible for memory management in the C # program. Because both the memory and applications are managed, it is necessary to enhance type security to ensure application stability.
For C ++ programmers, exception handling is not new, but it is the main function of C. The difference between C # Exception Handling and C ++ is that it is a cross-language (another function at runtime ). Before C # is available, you must handle weird HRESULTs, but now it's all over because of exception-based robust error handling.
For modern applications, security is the top priority, and C # is no exception. It provides metadata syntax to declare the capabilities and permissions of the following NGWS security mode. Metadata is a key concept for running NGWS. The next chapter will introduce its deeper meaning.

Object-oriented
You don't expect that a new language does not support object-oriented functions, right? C # certainly supports 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) When NGWS is running. VOS is described in the following chapter. The object mode is only a basic part and is no longer part of the programming language.
The first thing you must pay attention to is that there are no global functions, variables, or constants. All things are encapsulated in the class, including case members (accessible through the class case-object) or all static members (through the data type ). 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 rewritten by the derived class ). The main argument is that this will eliminate other original code errors caused by occasional method rewriting. To rewrite the method, you must have an explicit virtual flag. This behavior not only slows down the virtual function table, but also ensures correct version control.
Using C ++ to write classes, you can use access modifiers to set different access levels for class members. C # supports three access permissions: private, protected, and public, and adds the fourth type: internal. Details about access permissions are described in Chapter 5 "class.
How many classes have you created are derived from multiple base classes (ATL programmer, your vote is not counted !) ? In most cases, you only need to derive from a class. Multi-base classes usually cause more trouble than they solve. That is why C # only allows one base class. If you think you need multiple inheritance, you can use the interface.
A possible problem: a pointer does not exist in C #. How can we imitate it? The answer to this question is representative. It provides support for the NGWS running current event mode. Again, I will put the full explanation of it in chapter 5.

Type Security
I select a pointer again as an example. With a pointer in C ++, You can freely convert it to any type, including producing a pointer such as an int * (integer pointer) forced conversion into a double * (double Precision pointer. As long as the memory supports this operation, it is "dry ". This is not the type security you think of as an enterprise-level programming language.
Outline the problem, C # implements the strictest type of security to protect yourself and the garbage collector ). Therefore, you must follow the rules of some related variables in C:
You cannot use a variable without initialization. The compiler is responsible for clearing member variables of objects. You are responsible for resetting local variables. When you use a variable without initialization, the compiler will teach you how to do it. The advantage is to avoid errors caused by the calculation results of uninitialized variables, and you do not know how these strange results are produced.
C # The unsafe type conversion is canceled. You cannot forcibly convert an integer into a reference type (such as an object). When downconversion is performed, C # verifies that the conversion is correct. (That is, the derived class is actually derived from the class that is converted down .)
Border Check is part of C. This will never happen again: When the array actually only defines n-1 elements, n elements are used in excess.
Arithmetic Operations may overflow the range of the final value data type. C # allows you to detect these operations at the statement or application level. When overflow detection is allowed, an exception is thrown when overflow occurs.
In C #, the passed reference parameters are type-safe.

Versionable)
In the past few years, almost all programmers have to deal with the well-known "DLL hell" at least once ". This problem occurs when multiple applications have different versions with the same DLL name installed. Sometimes, older versions of applications can work well with new versions of DLL, but more often they interrupt the operation. The current version issue is really a headache.
As you will see in Chapter 8 "Write components in C #", NGWS runtime provides version support for your applications. C # It is best to support version control. Although C # cannot ensure correct version control, it is possible for programmers to ensure version control. With this support, a developer can ensure that the binary compatibility with existing customer applications is retained when the class library is upgraded.

Compatible
C # does not exist in a closed world. It allows access to different APIs using the most advanced NGWS Common Language Specification (CLS. CLS specifies a standard for internal operations in a language that complies with this standard. To enhance CLS compilation, the C # compiler detects all public exit compilation and lists errors when they are out of date.
Of course, you also want to access the old COM object. NGWS provides transparent access to COM during runtime. How to integrate the original code is described in Chapter 10th "internal operations on non-managed code.
OLE Automation is a special animal. Anyone who creates an OLE automation project using C ++ already enjoys various types of automated data. The good news is that C # supports them without the hassle of locking details.
Finally, C # allows you to use the C-prototype API to perform internal operations. You can access any DLL entry point (with a C prototype) from your application ). The function used to access the original API is called the platform call service (PInovke). Chapter 10th describes some examples of using the c api for internal operations.

Flexible
The last section of the previous section may remind programmers. You may ask, "Isn't there an API for passing pointers? "You are correct. There are not only a few such APIs, but many (somewhat conservative estimates ). This access to the original WIN32 code sometimes leads to the use of the specified pointer for the unsafe class (although some of them can be solved by the support of COM and PInvoke ).
Although the default status of C # code is type-safe, you can declare some classes or declare methods of only classes as non-secure types. Such a statement allows you to use pointers and structures to statically allocate arrays. Both the security code and the non-security code run in the same management space, which implies that when the non-security code is called from the security code, it will not fall into the column set ).

Summary
C # Language evolved from C and C ++. It is designed for enterprise developers who are willing to sacrifice C ++ some underlying functions for convenience and productization.

Related Article

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.