Disclaimer, the original copyright belongs to Microsoft, this article is only convenient for many netizens to familiarize with the upcoming C / CLI technology. Translator: jiang_hgame, JustLikeTheWind8. Language Overview Tip: This specification is an extension of the C standard. This suggestions illustrate the necessary characteristics of the model. The subsequent sections will be aware of the rules and exceptions. This chapter will exchange simplicity and clearness at the expense of integrity. We tried to provide a presentation to promote readers to write some preliminary programs and read the following chapters. 8.1 Start-specification "Hello World" program can be written as the following punishment: int main () {system :: console :: WriteLine ("Hello, World");} The source code of the C / CLI program is stored in One or more of the CPP is the text file like Hello.cpp. Use a life
Letter compiler (for example, CL). Program can be compiled by such a command: CL hello.cpp will generate an application file hello.exe, run the file to generate the output Hello, the World / N Cli library consists of a large number of namespace, where The most commonly used is System, which contains a reference class console, which provides a set of functions for running the console input and output, WriteLine is one of these classes, when entering a set of strings, the console will output String and wrap. (Note that we assume that the namespace of this example has been declared by the USING statement) 8.2types Value Type and handle type The difference between the value type directly contains their data, and the handle type variable stores the handle of the object. For handle types, two variables may reference the same object. This affects the operation of one of the variables affects the object, which affects another variable. For value types, each variable has its own data copy, and the operation of a variable does not affect another variable. Example REF class class1 {public: int value; class1 () {value = 0;}}; int main () {int val1 = 0; int val2 = VAL1; VAL2 = 123; Class1 ^ ref1 = gcnew class1; class1 ^ ref2 = Ref1; Ref2-> value = 123; console :: WriteLine ("VALUES: {0}, {1}", VAL1, VAL2); Console :: WriteLine ("refs: {0}, {1}", ref1 -> value, ref2-> value);} From the output result, it can be seen that the difference between the two values: 0, 123REFS: 123, 123 on the local variable VAL1 does not affect the local variable VAL2, because two local variables Both of the value type, the local variables of each value type have their own storage space, the opposite, the assignment of REF2-> value = 123, affects the objects of REF1 and REF2 common references. Console :: WriteLine ("VALUES: {0}, {1}", VAL1, VAL2); Console :: WriteLine ("refs: {0}, {1}", ref1-> value, ref2-> value); The above lines are worth doing more annotations, they showcase some console :: WriteLine string formatted behavior. In fact, it uses variable number of parameters. The first parameter is a string that contains a certain number of placeholders such as {0} and {1}. Each placeholder references the parameters of the back, such as {0} reference the second parameter. {1} Reference third parameters and push it with a subkey. Before the output is sent to the console, each placeholder will be replaced by the value of the formatted parameters it corresponds. Developers can define new value types through the declaration of ENUM and value types.