Differences between C ++ and C # Projects

Source: Internet
Author: User
The
Function Reference topic

Inheritance: In C ++, classes and structures are actually the same, while in C #, they are very different. C # class can implement any number of interfaces, but can only inherit from one base class. In addition, the C # structure does not support inheritance or explicit default constructor (one is provided by default ).

Class

Interface

Struct (C # reference)

Array: In C ++, an array is just a pointer. In C #, arrays are objects that contain methods and attributes. For example, you can use the Length attribute to query the size of an array. The C # array also uses the indexer (verify that each index used to access the array ). The syntax for declaring the C # array is different from that for declaring the C ++ array: in C #, the "[]" mark appears after the array type, instead of after the variable.

Array (C # programming guide)

Indexer (C # programming guide)

Boolean value: In C ++,BoolType is essentially an integer. In C #, it does not exist.BoolConversion between types and other types.

Bool

LongType:LongThe type is 64-bit in C #, and 32-bit in C ++.

Long

Passing parameters: In C ++, all variables are passed through values unless explicitly passed through pointers or references. In C #, unlessRefOrOutParameter modifier references. Otherwise, the class is passed through references, while the structure is passed through values.

Structure

Class

Ref (C # reference)

Out (C # reference)

SwitchStatement:SwitchC # does not support running from one case tag to another.

Switch

Delegate: the C # delegate is similar to the function pointer in C ++ and is safe and reliable.

Delegate

Base class method: C # supports calling a derived class to override base class members.BaseKeyword. In C #,OverrideKeyword rewriting virtual or abstract methods are explicit.

Base

See override examples

Method hiding: C ++ implicitly hides the supported methods by inheriting them ". In C #, you must useNewModifier to explicitly hide inherited members.

New

Preprocessor commands are used for Conditional compilation. C # does not use header files.

C # Preprocessor commands

Exception Handling: No matter whether an exception is thrown or not, C # providesFinallyKeyword to provide the executableCode.

Try-finally

Try-catch-finally

C # Operator: C # supports other operators, suchIsAndTypeof. It also introduces different functions of some logical operators.

& Operators

| Operator

^ Operator

Is

Typeof

ExternKeywords: In C ++,ExternUsed for the import type. In C,ExternUsed to use the sameProgramCreate aliases for different versions of the set.

Extern

StaticKeywords: In C ++,StaticIt can be used to declare class-level entities or declare types specific to a module. In C,StaticIt is only used to declare class-level entities.

Static

C #MainMethod and C ++MainFunction declaration methods are different. In C #, It is capitalized and alwaysStatic. In addition, in C #, the support for processing command line parameters is much more reliable.

Main () and command line parameters (C # programming guide)

In C #, onlyUnsafePointer is allowed.

Unsafe

In C #, the overload operator is executed in different ways.

C # Operator

String: In C ++, a string is only an array of characters. In C #, strings are objects that support reliable search methods.

String

String

ForeachKeyword allows you to access arrays and collections cyclically.

Foreach, in

Global: Global methods and global variables are not supported in C. Methods and variables must be included inClassOrStruct.

C # general program structure

Import Type: In C ++, common types of multiple modules are placed in header files. In C #, you can obtain this information through metadata.

Using

Metadata Overview

Local variables in C # cannot be used before initialization.

Method (C # programming guide)

Memory Management: The C ++ language does not provide the garbage collection function. before the process is terminated, the memory that has not been explicitly released remains allocated. C # language provides the garbage collection function.

Garbage Collection

Destructor: C # has different syntaxes used to release unmanaged resources.

Destructor

Using Statement (C # reference)

Constructor: similar to C ++, if Class constructor is not provided in C #, a default constructor is automatically generated for you. The default constructor initializes all fields as their default values.

Instance Constructor

Default table

C # bit fields are not supported.

C ++ Bit Field

The input/output service and format settings of C # depend on the Runtime Library of. NET Framework.

C # Language tutorial

Format the numeric result table

In C #, method parameters cannot have default values. To achieve the same effect, use method overload.

Compiler error cs0241

In C #, the generic type and generic method provided for Type parameterization are similar to the C ++ template, despite significant differences.

Generic Type in C #

as keyword is similar to the standard forced conversion keyword. The difference is that if the conversion fails, the return value is null rather than an exception. This is different from using static_cast (unlike dynamic_cast in C ++, it does not perform a runtime check, therefore, no exception is thrown when a failure occurs.) Similar.

As (C # reference)

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.