コード例 #1
0
ファイル: RandomExt.cs プロジェクト: wendell620/Cyjb
 /// <summary>
 /// 返回一个小于所指定最大值的非负随机数。
 /// </summary>
 /// <param name="maxValue">要生成的随机数的上限(随机数不能取该上限值)。
 /// <paramref name="maxValue"/> 必须大于或等于零。</param>
 /// <returns>大于等于零且小于 <paramref name="maxValue"/> 的 <c>32</c> 位有符号整数,
 /// 即:返回值的范围通常包括零但不包括 <paramref name="maxValue"/>。
 /// 不过,如果 <paramref name="maxValue"/> 等于零,则返回 <paramref name="maxValue"/>。
 /// </returns>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="maxValue"/> 小于零。</exception>
 public static int Next(int maxValue)
 {
     if (maxValue < 0)
     {
         throw CommonExceptions.ArgumentNegative("maxValue", maxValue);
     }
     Contract.Ensures(Contract.Result <int>() >= 0 && Contract.Result <int>() <= maxValue);
     return(Random.Next(maxValue));
 }
コード例 #2
0
 /// <summary>
 /// 返回指定字符串中位于指定位置的字符的宽度。
 /// </summary>
 /// <param name="str">要获取宽度的字符字符串。</param>
 /// <param name="index"><paramref name="str"/> 中的字符位置。</param>
 /// <returns><paramref name="str"/> 中指定字符的宽度,结果可能是 <c>0</c>、<c>1</c> 或 <c>2</c>。</returns>
 /// <remarks>此方法基于 Unicode 标准 6.3 版。详情请参见
 /// <see href="http://www.unicode.org/reports/tr11/">Unicode Standard Annex #11 EAST ASIAN WIDTH</see>。
 /// 返回 <c>0</c> 表示控制字符、非间距字符或格式字符,<c>1</c> 表示半角字符,
 /// <c>2</c> 表示全角字符。</remarks>
 /// <exception cref="ArgumentNullException"><paramref name="str"/> 为 <c>null</c>。</exception>
 /// <exception cref="IndexOutOfRangeException"><paramref name="index"/> 大于等于字符串的长度或小于零。</exception>
 /// <seealso href="http://www.unicode.org/reports/tr11/">Unicode Standard Annex #11 EAST ASIAN WIDTH</seealso>。
 public static int Width(string str, int index)
 {
     CommonExceptions.CheckArgumentNull(str, "str");
     if (index < 0)
     {
         throw CommonExceptions.ArgumentNegative("index", index);
     }
     if (index >= str.Length)
     {
         throw CommonExceptions.ArgumentOutOfRange("index", index);
     }
     Contract.Ensures(Contract.Result <int>() >= 0 && Contract.Result <int>() <= 2);
     return(Width(char.ConvertToUtf32(str, index)));
 }