C ++ vs c # (2): String, namespace

Source: Internet
Author: User

// ================================================ ====================================
// TITLE:
// C ++ vs c # (2): String, namespace
// AUTHOR:
// Norains
// DATE:
// Tuesday 30-November-2010
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// ================================================ ====================================

1. String

Both C ++ and C # support strings and can be saved using strings. However, string is a built-in type in C #, but STL must be used in C ++. Another interesting thing is that string stores UNICODE in C #, but in C ++ it is ANSIC. If you want C ++ to support UNICODE strings, you must use wstring.

You can perform the following operations on strings in these two languages: string strVal = "Hello ,";
StrVal + = "World !";

If a string contains a line break or other Conversion characters, it can be expressed by "", for example, strVal = "Hello, World! I like it !";
StrVal = "He said:" It's you !"";

However, C # has an enhanced feature. You can add "@" before the first quotation mark to indicate that the content of the quotation marks before ";" after "@" is intact as a numerical value. To put it simply, the preceding two statements can also be expressed in the form of C #: strVal = @ "Hello, World!
I like it !";
StrVal = @ "He said:" It's you !"";


2. namespace

Both C ++ and C # support namespaces, that is, namespaces, and nesting, such as namespace Root.
{
Namespace Leaf
{
Enum Type
{
TYPE_NORMAL,
TYPE_HIGH,
};
}
}
 

Although the statement is consistent, the usage is quite different. For C ++, The namespace must be labeled with ":", for example, Root: Leaf: Type type = Root: Leaf: TYPE_NORMAL;

However, C # uses ".": Root. Leaf. Type = Root. Leaf. type. TYPE_NORMAL;

From the operational point of view, the Type in this example is more like the class Type in C ++, while C # Is the object Type.

Another interesting difference is that C # cannot directly declare variables or functions in the namespace, such:
Namespace Root
{
Namespace Leaf
{
Int iVal = 0;

Void Run (){};
}
}
 

This code segment can be compiled perfectly in C ++, but an error is prompted in C #: error CS0116: A namespace cannot directly contain members such as fields or methods

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.