C # Advanced Programming, chapter II, Core C # Programming--Learning Notes

Source: Internet
Author: User

Before preparing to save the race, no time to learn this. Now the province is over, the time is a little bit more so be prepared to learn a little, because the performance of the provincial race is not very good, so I should still spend the main time on the algorithm. C # can only take a few minutes a week to see. Because there is not too much time to learn, so I want to blog the form of a record of their own learning process. At the same time in summing up the process, it should be able to make my understanding of it will be more profound. ---preface

Say, "Hello world!." is the first program that every programmer touches a language, of course, as a future programmer, I am no exception, my "Hello world!"

Using System;namespace myfirstprogram{    class program    {        static void Main (string[] args)        {            Console.WriteLine ("Hello world!");            Console.readkey ();}}}    

Although "Hello world!" has been written several times, but still have a feeling of excitement. (Maybe I'm a little bit low)

C # As the beginning of C language, its C-style, or to learn c\c++ classmates more friendly, feel more comfortable.

C # contrasts with c\c++, the difference:

1. The variable has more than one type to infer the VAR keyword. The compiler can infer the type of the variable based on the initialization value of the variable.

var a = "Hello"; Like this, the compiler infers that the variable s is of type string

2, the predefined data types, C # is divided into value types and reference types, value types and c\c++ is similar, reference types feel a little new, actually a bit like C + + reference

Using System;namespace myfirstprogram{    class Vector    {public        int value;    }    Class program    {        static void Main (string[] args)        {            Vector x, y;            x = new Vector ();            X.value = +;            y = x;            Console.WriteLine (y.value);            Y.value =;            Console.WriteLine (x.value);            Console.readkey ();}}}    

The output results are 30 and 50. Have you found anything, believe you have found that the above running results tell us that the above code is only one object. Why is it?

Because you must use the New keyword in C # to create a true pair of images.

Vector x, y;   If this is the case, then the system will not create a real object

For example, the string class, in C #, a string type is a reference type. The string object is allocated on the heap, not on the stack. When you assign another string to a string variable, you get two references to the same string in memory

String str1, str2;str1 = "Hello world"; str2 = str1;

Like the above example, STR1,STR2 is a reference to "Hello World" and not a real object. But there are some differences between string and other common reference types: for example, a string cannot be modified. Modifying one of the strings will create a completely new string object, and the other string will not change. C # also introduces a better approach to character output like slashes. Add ' @ ' before the string, and all characters after this character have only his original meaning----they are not interpreted as escape characters:

String filePath = @ "C:\CSharp\MyfirstProgram.cs";

3. On the bool type, C # does not use 1 as true, and 0 as false. The value of bool is True and False, not 1 and 0.

4, in the loop control, it has a more than a foreach loop. foreach iterates through each item in the collection.

5. C # differs from the C + + switch: The emission order of the case clause of the ①c# switch is irrelevant, and even the default can be placed first. ② in C #, you can use a string as a test variable.

6, on the note, C # supports C\c++ 's annotation method, and introduces a document description in XML format, which starts with 3 slashes (///) and is a single-line comment. The C # compiler can extract an XML element from a specific comment and make it generate an XML file. To have an assembly generate an XML document, you need to specify the/doc option at compile time followed by the file name created


The syntax style of C # is closer to C + +, and the above is some of the differences. But C # 's type-checking mechanism is much stricter than C + +. For example, if you use a variable that is not assigned, the compiler will give you an error. In C + +, only a single warning is used even if there are no warnings on the IDE.




C # Advanced Programming, chapter II, Core C # Programming--Learning Notes

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.