// ================================================ ====================================
// 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