Chapter 1 C # Introduction

Source: Internet
Author: User

Chapter 1 C # Introduction

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
In the field of computing, C # will become the main tool for compiling Next-Generation Windows Services (NGWS) applications.
Language. "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.
Tan. 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. Screen abandoned
The complexity of C ++ makes 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 only generate
A lot of trouble rather than benefits.
New features that make programming more convenient are strict type security, version control, and garbage collect. All these functions aim
Developed 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 ++)
C # is easy to use.
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 want
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 new
Operators are still difficult to learn. C # discard other operators and only use a single operator ".". What a programmer needs to understand
Is the annotation of the nested name.
You do not have to remember the implicit types based on different processor architectures, or even the ranges of various integer types. C # use a unified type system and discard the variable types of C ++
System. 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 boxing and unboxing mechanisms, processing a simple type as an object does not improve the performance. I will explain in detail the Ampersand and
Box, 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 original if statement is incorrect.
Incorrect value assignment is now compiled incorrectly because if statements only accept 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. In view
Redundant forms have been removed from the language, so 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 users can use C ++ to implement
Functions that can be easily implemented are only 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 dedicated to 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. NGWS provides
The garbage collector is 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
). Before Running C #, you must handle weird HRESULTs. However, due to the exception-based robust error handling
The switch ends.
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. Complete
The C # class mode of 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 the basis
It is no longer part of a 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 (by class
For example, an object can be accessed) or all static members (by 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 problems caused by occasional method rewriting.
Error code. 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 # private,
Protected and public access permissions, and added the fourth type: internal. For more information about access permissions, see section 5 "class ".
Ming.
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
Output. 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 run
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 the mode for running the current event on NGWS.
Yes. 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 are free to forcibly convert it to any type, including producing a pointer such as an int *
(Integer pointer) forced conversion into a double * (double Precision pointer) such silly thing. As long as the memory supports this operation, it is "dry ". This is not what you think
Enterprise-level programming language type security.
Outline the problem, C # implements the strictest type of security to protect yourself and the garbage collector (garbage collector ). Therefore, you must comply with some of C #'s related requirements.
Variable rules:
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
When there are initialization variables, the compiler will teach you how to do this. The advantage is that you can avoid errors caused by the calculation results of non-initialized variables.
Know how these strange results are produced.
C # The unsafe type conversion is canceled. An integer cannot be forcibly converted to a reference type (such as an object), while C # verifies that the conversion is positive
Yes. (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 is detected
An exception will be thrown during the departure.
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 are installed.
Different versions with the same DLL name. Sometimes, older versions of applications can work well with new versions of DLL, but more often they will interrupt the operation
Line. 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 check
When the class library is upgraded, the binary compatibility with existing customer applications is retained.

Compatible
C # does not exist in a closed world. It allows the use of the most advanced NGWS general Language Specification (Common Language Specification, abbreviated
CLS) access different APIs. CLS specifies a standard for internal operations in a language that complies with this standard. To enhance CLS compilation, C # compiler checks
Test all public exit compilation and list errors when the exit times out.
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 will be in Chapter 10th "unmanaged code
Is described.
OLE Automation is a special animal. Anyone who creates an OLE automation project using C ++ already enjoys various types of automated data. There is a good consumer
It means 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 ). Used to access the original
The API function is called the platform call service (PInovke). Chapter 10th describes how to use the c api for internal operations.
Some examples.

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. Not only a few
But a lot (a bit conservative ). This access to the original WIN32 code sometimes leads to the use of the specified pointer for the unsafe class (although
Some

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.