Possible Duplicate:
String vs string in C#可能重复:字符串vs字符串c#
Yeah, I knew that both of 2 style is ok, but which one is better, and why?
是的,我知道两种风格都可以,但是哪一种更好,为什么?
4 个解决方案
#1
1
In most cases, it is subject to opinion.
在大多数情况下,这取决于意见。
The less disputable facts:
越有争议的事实:
- Always use whichever your team already uses, or whichever your coding standard mandates
- 始终使用您的团队已经使用的任何一个,或任何您的编码标准要求
- In favor of the keyword style, it doesn't require a
using
declaration. - 对于关键字样式,它不需要使用声明。
- In output from a code generator, using the fully qualified name is less of a pain to code; otherwise you have to include special handling for keyword types
- 在代码生成器的输出中,使用完全限定的名称对代码来说不那么麻烦;否则,您必须包含对关键字类型的特殊处理
- In naming properties or methods, you should use the type name, not the keyword name. It must be translatable across .Net languages
- 在命名属性或方法时,应该使用类型名称,而不是关键字名称。它必须可以跨。net语言翻译
If it is a new project, and you are in control of the coding style, use whichever tickles your fancy.
如果是一个新项目,而您控制了编码风格,请使用您喜欢的任何一个。
In addition to the method/property naming requirement, I've seen people say they prefer the non-keyword type name, so that it is more directly grokable across .Net languages. My opinion on this opinion is to write code to the language you are in.
除了方法/属性命名要求之外,我还看到人们说他们更喜欢非关键字类型名称,这样它可以更直接地在。net语言中运行。我对这一观点的看法是,按照您所使用的语言编写代码。
#2
0
read this thread String vs string in C# which is about String and string
在c#中读取这个线程字符串和字符串,它是关于字符串和字符串的。
#3
0
The similar question was raised previously in *. And the Answer to it is given. Please refer to the following link for details:
类似的问题在*上曾被提过。答案已经给出。详情请参阅以下连结:
https://*.com/questions/215255/string-vs-string-in-c#
https://*.com/questions/215255/string-vs-string-in-c
#4
0
I'll add that StyleCop ( http://stylecop.codeplex.com/ ) "prefers" the "lower case" versions.
我将添加StyleCop (http://stylecop.codeplex.com/)喜欢“小写”版本。
SA1121 Readability Rules UseBuiltInTypeAlias
A violation of this rule occurs when one of the following types are used anywhere in the code: Array, Boolean, Byte, Char, Decimal, Double, Int16, Int32, Int64, Object, SByte, Single, String, UInt16, UInt32, UInt64.
当在代码中的任何地方使用以下类型之一时,就会出现违反此规则的情况:数组、布尔、字节、字符、十进制、双精度、Int16、Int32、Int64、Object、SByte、Single、String、UInt16、UInt32、UInt64。
A violation also occurs when any of these types are represented in the code using the full namespace for the type: System.Array, System.Boolean, System.Byte, System.Char, System.Decimal, System.Double, System.Int16, System.Int32, System.Int64, System.Object, System.SByte, System.Single, System.String, System.UInt16, System.UInt32, System.UInt64.
当在代码中使用类型:System的完整名称空间表示任何这些类型时,也会发生违背。数组,系统。布尔值系统。字节,系统。字符,系统。小数,系统。双系统。Int16、系统。Int32,系统。Int64、系统。对象,系统。SByte、系统。单,系统。字符串,系统。UInt16、系统。UInt32 System.UInt64。
Rather than using the type name or the fully-qualified type name, the built-in aliases for these types should always be used: array, bool, byte, char, decimal, double, short, int, long, object, sbyte, single, string, ushort, uint, ulong.
与其使用类型名或完全限定类型名,不如使用这些类型的内置别名:数组、bool、字节、char、decimal、double、short、int、long、object、sbyte、single、string、ushort、uint、ulong。
(taken from the help file StyleCop.chm)
(取自帮助文件StyleCop.chm)
#1
1
In most cases, it is subject to opinion.
在大多数情况下,这取决于意见。
The less disputable facts:
越有争议的事实:
- Always use whichever your team already uses, or whichever your coding standard mandates
- 始终使用您的团队已经使用的任何一个,或任何您的编码标准要求
- In favor of the keyword style, it doesn't require a
using
declaration. - 对于关键字样式,它不需要使用声明。
- In output from a code generator, using the fully qualified name is less of a pain to code; otherwise you have to include special handling for keyword types
- 在代码生成器的输出中,使用完全限定的名称对代码来说不那么麻烦;否则,您必须包含对关键字类型的特殊处理
- In naming properties or methods, you should use the type name, not the keyword name. It must be translatable across .Net languages
- 在命名属性或方法时,应该使用类型名称,而不是关键字名称。它必须可以跨。net语言翻译
If it is a new project, and you are in control of the coding style, use whichever tickles your fancy.
如果是一个新项目,而您控制了编码风格,请使用您喜欢的任何一个。
In addition to the method/property naming requirement, I've seen people say they prefer the non-keyword type name, so that it is more directly grokable across .Net languages. My opinion on this opinion is to write code to the language you are in.
除了方法/属性命名要求之外,我还看到人们说他们更喜欢非关键字类型名称,这样它可以更直接地在。net语言中运行。我对这一观点的看法是,按照您所使用的语言编写代码。
#2
0
read this thread String vs string in C# which is about String and string
在c#中读取这个线程字符串和字符串,它是关于字符串和字符串的。
#3
0
The similar question was raised previously in *. And the Answer to it is given. Please refer to the following link for details:
类似的问题在*上曾被提过。答案已经给出。详情请参阅以下连结:
https://*.com/questions/215255/string-vs-string-in-c#
https://*.com/questions/215255/string-vs-string-in-c
#4
0
I'll add that StyleCop ( http://stylecop.codeplex.com/ ) "prefers" the "lower case" versions.
我将添加StyleCop (http://stylecop.codeplex.com/)喜欢“小写”版本。
SA1121 Readability Rules UseBuiltInTypeAlias
A violation of this rule occurs when one of the following types are used anywhere in the code: Array, Boolean, Byte, Char, Decimal, Double, Int16, Int32, Int64, Object, SByte, Single, String, UInt16, UInt32, UInt64.
当在代码中的任何地方使用以下类型之一时,就会出现违反此规则的情况:数组、布尔、字节、字符、十进制、双精度、Int16、Int32、Int64、Object、SByte、Single、String、UInt16、UInt32、UInt64。
A violation also occurs when any of these types are represented in the code using the full namespace for the type: System.Array, System.Boolean, System.Byte, System.Char, System.Decimal, System.Double, System.Int16, System.Int32, System.Int64, System.Object, System.SByte, System.Single, System.String, System.UInt16, System.UInt32, System.UInt64.
当在代码中使用类型:System的完整名称空间表示任何这些类型时,也会发生违背。数组,系统。布尔值系统。字节,系统。字符,系统。小数,系统。双系统。Int16、系统。Int32,系统。Int64、系统。对象,系统。SByte、系统。单,系统。字符串,系统。UInt16、系统。UInt32 System.UInt64。
Rather than using the type name or the fully-qualified type name, the built-in aliases for these types should always be used: array, bool, byte, char, decimal, double, short, int, long, object, sbyte, single, string, ushort, uint, ulong.
与其使用类型名或完全限定类型名,不如使用这些类型的内置别名:数组、bool、字节、char、decimal、double、short、int、long、object、sbyte、single、string、ushort、uint、ulong。
(taken from the help file StyleCop.chm)
(取自帮助文件StyleCop.chm)