From 56a6a368ecaa81b1bdd0d4d8c23454ef53f9644a Mon Sep 17 00:00:00 2001 From: Yami An Date: Wed, 18 Jan 2023 17:18:03 +0700 Subject: [PATCH] 230118 --- YANLib/YANDateTime.cs | 19 ++++++------------- YANLib/YANList.cs | 12 +----------- YANLib/YANNum.Decimal.cs | 6 +++++- YANLib/YANNum.Double.cs | 6 +++++- YANLib/YANNum.Float.cs | 6 +++++- YANLib/YANNum.Int.cs | 6 +++++- YANLib/YANNum.Nint.cs | 6 +++++- YANLib/YANNum.Sbyte.cs | 6 +++++- YANLib/YANNum.Short.cs | 6 +++++- 9 files changed, 42 insertions(+), 31 deletions(-) diff --git a/YANLib/YANDateTime.cs b/YANLib/YANDateTime.cs index 879c3490..3225c02f 100644 --- a/YANLib/YANDateTime.cs +++ b/YANLib/YANDateTime.cs @@ -1,15 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using static System.Globalization.DateTimeFormatInfo; +using static System.DateTime; using static System.Globalization.CultureInfo; +using static System.Globalization.DateTimeFormatInfo; using static System.Globalization.DateTimeStyles; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static System.DateTime; -using System.ComponentModel.DataAnnotations; -using System.Runtime.Intrinsics.X86; +using static System.Math; namespace YANLib; @@ -77,10 +70,10 @@ public static class YANDateTime public static DateTime RandomDateTime(DateTime min, DateTime max) => min > max ? Today : min.AddDays(new Random().Next((MaxValue - MinValue).Days)); /// - /// Get week of year advanced. + /// Returns the week of year that includes the date in the specified value. /// /// Input datetime. - /// Number week of year. + /// The positive integer that represents the week of the year that includes the date in the parameter. public static int GetWeekOfYear(this DateTime dt) => CurrentInfo.Calendar.GetWeekOfYear(dt, CurrentInfo.CalendarWeekRule, CurrentInfo.FirstDayOfWeek); /// @@ -89,5 +82,5 @@ public static class YANDateTime /// /// /// - public static int TotalMonths(DateTime dt1, DateTime dt2) => Math.Abs((dt1.Year - dt2.Year) * 12 + dt1.Month - dt2.Month); + public static int TotalMonths(DateTime dt1, DateTime dt2) => Abs((dt1.Year - dt2.Year) * 12 + dt1.Month - dt2.Month); } diff --git a/YANLib/YANList.cs b/YANLib/YANList.cs index aa07ec38..8b8ca26b 100644 --- a/YANLib/YANList.cs +++ b/YANLib/YANList.cs @@ -10,17 +10,7 @@ public static class YANList /// Object type. /// Input list. /// List has item or not. - public static bool HasItem(this List list) - { - if (list != null) - { - if (list.Count > 0) - { - return true; - } - } - return false; - } + public static bool HasItem(this List list) => list != null && list.Count > 0; /// /// Split list by size. diff --git a/YANLib/YANNum.Decimal.cs b/YANLib/YANNum.Decimal.cs index df51caf8..76f97f15 100644 --- a/YANLib/YANNum.Decimal.cs +++ b/YANLib/YANNum.Decimal.cs @@ -46,7 +46,11 @@ public static decimal ParseDecimal(this string str) /// /// The exclusive upper bound of the random number to be generated. must be greater than or equal to 0. If not, the inclusive lower bound of the random number flexible to . /// Decimal random number. - public static decimal RandomNumberDecimal(decimal max) => max < 0 ? new Random().NextDecimal(decimal.MinValue, max) : new Random().NextDecimal(0, max); + public static decimal RandomNumberDecimal(decimal max) + { + var rnd = new Random(); + return max < 0 ? rnd.NextDecimal(decimal.MinValue, max) : rnd.NextDecimal(0, max); + } /// /// Generate random decimal number with min and max value. diff --git a/YANLib/YANNum.Double.cs b/YANLib/YANNum.Double.cs index 2dbf024c..7ca23b1a 100644 --- a/YANLib/YANNum.Double.cs +++ b/YANLib/YANNum.Double.cs @@ -46,7 +46,11 @@ public static double ParseDouble(this string str) /// /// The exclusive upper bound of the random number to be generated. must be greater than or equal to 0. If not, the inclusive lower bound of the random number flexible to . /// Double random number. - public static double RandomNumberDouble(double max) => max < 0 ? new Random().NextDouble(double.MinValue, max) : new Random().NextDouble(0, max); + public static double RandomNumberDouble(double max) + { + var rnd = new Random(); + return max < 0 ? rnd.NextDouble(double.MinValue, max) : rnd.NextDouble(0, max); + } /// /// Generate random double number with min and max value. diff --git a/YANLib/YANNum.Float.cs b/YANLib/YANNum.Float.cs index 12ba2932..ad649011 100644 --- a/YANLib/YANNum.Float.cs +++ b/YANLib/YANNum.Float.cs @@ -46,7 +46,11 @@ public static float ParseFloat(this string str) /// /// The exclusive upper bound of the random number to be generated. must be greater than or equal to 0. If not, the inclusive lower bound of the random number flexible to . /// Float random number. - public static float RandomNumberFloat(float max) => max < 0 ? new Random().NextSingle(float.MinValue, max) : new Random().NextSingle(0, max); + public static float RandomNumberFloat(float max) + { + var rnd = new Random(); + return max < 0 ? rnd.NextSingle(float.MinValue, max) : rnd.NextSingle(0, max); + } /// /// Generate random float number with min and max value. diff --git a/YANLib/YANNum.Int.cs b/YANLib/YANNum.Int.cs index 1f183150..837eda6c 100644 --- a/YANLib/YANNum.Int.cs +++ b/YANLib/YANNum.Int.cs @@ -46,7 +46,11 @@ public static int ParseInt(this string str) /// /// The exclusive upper bound of the random number to be generated. must be greater than or equal to 0. If not, the inclusive lower bound of the random number flexible to . /// Int random number. - public static int RandomNumberInt(int max) => max < 0 ? new Random().Next(int.MinValue, max) : new Random().Next(0, max); + public static int RandomNumberInt(int max) + { + var rnd = new Random(); + return max < 0 ? rnd.Next(int.MinValue, max) : rnd.Next(0, max); + } /// /// Generate random int number with min and max value. diff --git a/YANLib/YANNum.Nint.cs b/YANLib/YANNum.Nint.cs index d93f7f5c..9b71488e 100644 --- a/YANLib/YANNum.Nint.cs +++ b/YANLib/YANNum.Nint.cs @@ -46,7 +46,11 @@ public static nint ParseNint(this string str) /// /// The exclusive upper bound of the random number to be generated. must be greater than or equal to 0. If not, the inclusive lower bound of the random number flexible to . /// Nint random number. - public static nint RandomNumberNint(nint max) => (nint)(max < 0 ? new Random().NextInt64(nint.MinValue, max) : new Random().NextInt64(0, max)); + public static nint RandomNumberNint(nint max) + { + var rnd = new Random(); + return (nint)(max < 0 ? rnd.NextInt64(nint.MinValue, max) : rnd.NextInt64(0, max)); + } /// /// Generate random nint number with min and max value. diff --git a/YANLib/YANNum.Sbyte.cs b/YANLib/YANNum.Sbyte.cs index 4c54251d..85cef181 100644 --- a/YANLib/YANNum.Sbyte.cs +++ b/YANLib/YANNum.Sbyte.cs @@ -46,7 +46,11 @@ public static sbyte ParseSbyte(this string str) /// /// The exclusive upper bound of the random number to be generated. must be greater than or equal to 0. If not, the inclusive lower bound of the random number flexible to . /// Sbyte random number. - public static sbyte RandomNumberSbyte(sbyte max) => (sbyte)(max < 0 ? new Random().Next(sbyte.MinValue, max) : new Random().Next(0, max)); + public static sbyte RandomNumberSbyte(sbyte max) + { + var rnd = new Random(); + return (sbyte)(max < 0 ? rnd.Next(sbyte.MinValue, max) : rnd.Next(0, max)); + } /// /// Generate random sbyte number with min and max value. diff --git a/YANLib/YANNum.Short.cs b/YANLib/YANNum.Short.cs index e2c71132..4a52ab15 100644 --- a/YANLib/YANNum.Short.cs +++ b/YANLib/YANNum.Short.cs @@ -46,7 +46,11 @@ public static short ParseShort(this string str) /// /// The exclusive upper bound of the random number to be generated. must be greater than or equal to 0. If not, the inclusive lower bound of the random number flexible to . /// Short random number. - public static short RandomNumberShort(short max) => (short)(max < 0 ? new Random().Next(short.MinValue, max) : new Random().Next(0, max)); + public static short RandomNumberShort(short max) + { + var rnd = new Random(); + return (short)(max < 0 ? rnd.Next(short.MinValue, max) : rnd.Next(0, max)); + } /// /// Generate random short number with min and max value.