【发布时间】:2012-12-18 07:15:09
【问题描述】:
在The C# Programming Language (Covering C# 4.0) (4th Edition),1.3 类型和变量,第 9 页。
Jon Skeet 说;
万岁字节是无符号类型!在Java中一个字节的事实 已签名(并且没有未签名的等价物)会产生很多 毫无意义的比特玩弄容易出错。我们很有可能 都应该比我们更多地使用 uint,请注意:我确定 许多开发人员在需要整数时默认使用 int 类型。当然,框架设计者也属于这一类: 为什么要对 String.Length 进行签名?
当我反编译String.Length;
/// <summary>
/// Gets the number of characters in the current <see cref="T:System.String"/> object.
/// </summary>
///
/// <returns>
/// The number of characters in the current string.
/// </returns>
/// <filterpriority>1</filterpriority>
[__DynamicallyInvokable]
public int Length { [SecuritySafeCritical, __DynamicallyInvokable, MethodImpl(MethodImplOptions.InternalCall)] get; }
也在MSDN;
Length 属性返回此中 Char 对象的数量 例如,不是 Unicode 字符数。原因是一个 Unicode 字符可能由多个 Char 表示。
它返回当前字符串中字符对象的数量。为什么String.Length 已经存在UInt32 类型时返回Int32? signed-unsigned byte 和 String.Length 的意义何在?
【问题讨论】:
标签: c# .net unsigned signed string-length