Syntax and functionality differences between C + + and C #

Source: Internet
Author: User
Tags foreach arrays bool constructor exception handling header logical operators variables
c++| Difference | grammar

The following table contains important comparisons between C + + and C # features. If you are a C + + programmer, this table will provide you with the most important differences between the two languages.

Note C + + and C # projects are derived from different project models. For information about the differences between C + + and C # projects, see Item Management and using Solution Explorer in the project.
Functional reference topics
Inheritance: A class can only inherit an implementation from a base class. In addition, a class or interface can implement multiple interfaces.

Arrays: declaring C # arrays and declaring C + + arrays differ in syntax. In C #, the [] tag appears after the array type.
BOOL Type: There is no conversion between bool type and other types (especially int).
Long: In C #, a Long data type is 64 bits and 32 bits in C + +.
struct type: In C #, classes and structs are semantically different. struct is a value type, and class is a reference type.

Switch statement: Unlike switch statements in C + +, C # does not support running from one case label to another.
Delegate type: Delegates are basically similar to function pointers in C + +, but the former is type-safe and safe.
Overrides a base class member from a derived class call.

Use the new modifier modifier explicitly to hide an inherited member.
The declaration rewrite method requires a override keyword.
Preprocessor directives are used for conditional compilation. Header files are not used in C #.
Exception handling: Using the finally statement.

C # operators: C # supports other operators, such as IS and TypeOf. It also introduces the different functions of some logical operators.

Use of the keyword extern.
Use of the keyword static.
The method that replaces the C + + initialization list on the constructed base class.
The general structure of a C # program: namespaces, classes, structs, delegates, and enumerations.
The main method and the main function in C + + are declared differently.
Method parameters: C # supports ref and out parameters, which supersede pointers passing arguments by reference.

Use pointers only in unsafe mode in C #. Unsafe
Overloaded operators are executed in C # in different ways.
String: C # string is different from C + + string. String
The foreach keyword allows you to iterate through arrays and collections. foreach, in
There are no global methods and global variables in C #: Methods and variables must be contained in type declarations, such as class or struct.
There are no header files and #include directives in C #: The using directive is used to reference types in namespaces in other incompletely qualified type names.
Local variables in C # cannot be used before initialization.
destructor: In C #, the call time of the destructor cannot be controlled because the destructor is invoked automatically by the garbage collector.
Constructors: Similar to C + +, if a class constructor is not provided in C #, the default constructor is automatically generated for you. The default constructor initializes all fields to their default values.

C # does not support bit fields. C + + bit fields
C # 's input/output services and formatting depend on the Run-time library of the. NET Framework.

In C #, method parameters cannot have default values. If you want to get the same effect, use the method overload.



Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.