C # And C ++

Source: Internet
Author: User

I just converted a program from c # To c ++. Therefore, I have a basic understanding of c # And c ++. Of course, there are too many things in the book that I will not mention, and many underlying things cannot be felt during development. For example, CLR. Just look at the syntax. Haha.

1. C # It is much easier to use arrays than C ++, especially when the subscript is not a constant but determined only when the program is running. For example, a two-dimensional array, in C ++, we have to apply for the following space twice:

Int ** p = NULL;

P = new int * [width];

For (int I = 0; I <width; I ++)

P [I] = new int [height];

But in c #, you only need int [,] p = new int [width] [height]; a statement.

2. In terms of file read/write, c ++ is still much more powerful. It may be because c # does not have the pointer concept. Therefore, the c # File Read and Write function is basically readbyte (); readline (), either one byte to read, or read as a string, and then you parse the string. Binary read/write binaryreader and binarywriter provide readint32 (), readsingle (), and so on, but the file must be a binary file. Therefore, if we hand-written a text document, some data separated by spaces, including characters, floating point numbers, and so on, we cannot do the same as the original C ++,

Scantf ("% d % f ...", D, f );

I thought it was inconvenient to use this.

3. Function overload. C # is more convenient than C ++. For example, if I write a function swap () that exchanges two variables, if it is C ++, it may need swap (int, int), sway (float, float), etc, or use the template. C #, because all data types are inherited from objects, it is enough to use a function swap (object, object. Of course, there will be some box and unbox operations that affect efficiency, but for me writing programs, I still feel much more convenient.

4. For the "C # variable must be assigned before it is used", I feel that it may cause some inconvenience. For example, assign a value to a variable in a condition statement, even if the program writer can ensure that his or her condition statement is successful, the variable must be assigned a value. However, C # may encounter errors. You must explicitly assign values outside the Condition Statement.

E. g Point p;

If (a = 1)

P = new Point ();

....

Even if you can ensure that a = 1 is true, C # requires that p = new Point () be placed out of the Condition Statement. Of course, there is nothing very inconvenient in this example. I cannot think of a good example at the moment. Sorry.

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.