C # differs from C + +

Source: Internet
Author: User

C # is one of the three major mainstream OOP (object-oriented programming) language (c++,java,c#), is also the latest one, which must draw on the advantages of the former two, "or it's the creator of the Spanking"-in the Czech language, see "C#primer Chinese Version" of the translation of:-) they have too many similarities , its syntax and programming concepts, most of them are compatible with each other, three. Especially for programmers who are proficient in C + +, there is no difficulty in learning Java and C #, and the only place to take some time is to familiarize themselves with the class library.

Of the three, C + + is my most familiar and favorite, in the study of C # will inevitably compare it with C + +, and now the difference between C # and C + + do a summary. This is I spent about three hours to read the "C#primer Chinese version" of the first two chapters of the experience, inevitably back a lot of errors and deviations, write down, and then correct.

1. C # like Java, it is not the machine code that gets compiled, but the meta-instructions that run in the virtual machine. It has more security considerations, no pointers, no direct manipulation of memory, automatic memory management. While the pointers in C + + bring a lot of flexibility and efficiency, there are many problems in the use of C + + programs, most of which are derived from improper use of pointers, C # for software security considerations and language usability considerations without pointers.

C # An automatic garbage collection is implemented in the heap by new, and when the reference count for the object is 0 o'clock, the memory is reclaimed. The class has constructors but not enough functions.

C # There is no concept of pointers, only references and values. Internal data types such as int and struct define types are data types, deep copies are copied, and string and class-defined types are reference types, and shallow copies are made when copied-corresponding to deep copies, which implement object and memory management by reference counting.

C + + With the ability to easily implement pointers, C # needs to introduce many additional mechanisms. For example, a C + + function pointer, called Delegate in C #. In C #, parameter passing is divided into two types: transmit value and address, which need to be added ref or out (return change) keyword.

C # the const in is different from C + +, which refers to the compile-time constant, and the constant during the run is specified with ReadOnly.

2. C # OO features are more thorough, everything is object, there is no independent function, the entry Mai () function of the program is the public static member function of an object.

All objects are derived from object, including internal data type int,float,string, and so on, they are just aliases for System.Int32 and so on. There is no template in C #, which downcast similar to the dynamic_cast operator in C + + by setting the parameter to the object type for similar functionality.

C # there are no header files, and variables, functions, and classes are not defined and declared differently, all together. You can only implement code separation by designing an abstract class. C + +, although not perfect in this respect, is much stronger than C #.

C # attributes (properties) and Indexes (index). The GetValue property is similar to those in C + + () and SetValue () member functions, except for some differences in usage. The index is similar to the overloaded operator in C + + [].

C # is initialized at the member variable declaration in the class, and not in C + +, both of which can initialize the member variable in the constructor. Static member variables in C # can be initialized in a static constructor, and static constructors are called when the class is first used, while static member variables are initialized in C + + when the compilation unit is loaded.

3. C # The syntax for foreach (typevinlist) statement, while C + + standard library functions are required to implement similar functions.

C + + In contrast to C, many features that facilitate OOP are introduced, as well as features that are considered in the context of software engineering. Many C + + programmers (including me) are deeply in the spirit of its greatness. At the same time, it inherits the C language introduction and graceful, try to use a unified style to achieve as many features as possible, such as operator overloading, templates and so on. C #, compared to C + +, is more complex and bloated and messy.

Report:

  1. 1. C # does not support multiple inheritance, which is clearly different from the C + + place, said the real multi-inheritance sometimes used to be very troublesome, maybe Microsoft is afraid you use it, not to provide you, but I personally feel that there is no multiple inheritance feeling a little pity.
  2. 2. pointer-type operations are not supported in standard C # security code, but you can manipulate pointer-type objects in Microsoft's so-called "unsafe code".
  3. 3. all objects in C # can only be created by the keyword "new", and C + + 's "class name _ Object Name" method becomes a reference in C #. Hehe, everything objects, even the common data types have become objects, Java flavor is very strong.
  4. 4. the array becomes a class, so for the elements in the array. NETFramework directly provides a series of operations: Find, sort, invert ...
  5. 5. in C #, the elements of an array are stored in the managed heap, and it is much safer to apply for a contiguous space in an indeterminate location in memory than C + +.
  6. 6. C + + switch followed by parameter must be int type, and C # but allow string type, this improvement feels really more convenient than before!
  7. 7. C # disables all switch: If the case statement fails, the previous cases statement will be executed without a break, unless a box is followed by a space.
  8. 8. on exception handling, C + + allows any type to be thrown, while C # specifies that the throw type is an object derived from System.Exception.
  9. 9. C + + macros are often discarded in C # and are not recommended, so it is rare.
  10. the C + + template is not in C #, but in C # we find a sharper weapon that can accomplish the template task: the delegate.
  11. The concept of C + + Global variables is not, C # and Java are similar, to put everything in the class, it is also recommended to use a namespace to include.
  12. C # can assign a value to a property directly while defining a class, and C + + does this by compiling an error.
  13. in C # There is a concept of a static constructor that executes only once, thus guaranteeing that some static members are initialized only once.
  14. C # has an automatic garbage collection mechanism that prevents memory leaks and frees C + + programmers from heavy memory management.
  15. the stronger type conversion protection mechanism, such as converting float to uint, translates 0.35 directly into 0.34, because the binary cannot represent such a number, and using the System.conver method, you can safely convert the type safely. 。
  16. delegates and events, boxing and unboxing, Web Services ... A lot of new things that C + + does not have, I feel that it is easy to do large-scale project management, but also easy to expand, but do small projects, because it is compiled into IL code, running requires the. NET Framework SDK Support, efficiency is a problem, is not conducive to the computation speed and memory consumption of high requirements of the project.

C # differs from C + +

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.