C # and C
I just turned a program from C # to C languages, so there is a basic experience for C # and C comparison. Of course, there are too many kinds of books saying, and many underlying things are not perfect when we develop. Such as CLR. Light is a bit compared from the grammatical angle, and the tube is seen. Haha.
1. C # is much easier than C in the use of the array, especially if the subscript is not constant, but in the case where the program is run, such as a two-dimensional array, if we must apply a space twice in C as follows:
INT ** P = NULL;
P = new int * [width] ;;
For (int i = 0; i p [I] = new int [height]; But in C #, only INT [,] p = new int [width] [height]; one statement is OK. 2. On file reading and writing, C is still a lot of power. May be because C # does not have a pointer concept. Therefore, the C # file read and write function is basically readByte (); readline (), or one byte to read, or read in the form of a string, then you resolve this string. Binary read and write BinaryReader and binarywriter provides Readint32 (), readsingle (), etc., but requires that the file is binary. Therefore, if we write a text document, it is some data separated by spaces, including characters, floating point numbers, etc., we have no way like C , Scantf ("% D% f ... / n", d, f); I was very inconvenient at this time. L 3. The function is overloaded, C # is convenient than C . For example, I write a function swap () that swap two variables, if it is C , there may be SWAP (int, int), sway (float, float), etc., or use templates. C #, because all data types are inherited from Object, so it is enough to use a function SWAP (Object, Object). Of course, there will be some Box and UNBOX operational influence efficiency, but I am still more convenient for writing to the writing program. 4. This point about "C # variable must be assigned", personal feelings will bring some inconvenience. For example, in the conditional statement assignments, even if the writer can ensure that its conditional statement will succeed, it ensures that a variable is definitely assigned. But C # will be wrong, must require you to explicitly assign values outside the conditional statement. E.G Point P; IF (a == 1) P = new point (); . Even if you can guarantee A == 1 is established, C # also requires P = New Point () to place it outside the conditional statement. Of course, there is nothing very inconvenient in this example, I can't think of a good example. Sorry.