C#經常使用自界說函數小結。本站提示廣大學習愛好者:(C#經常使用自界說函數小結)文章只能為提供參考,不一定能成為您想要的結果。以下是C#經常使用自界說函數小結正文
本文實例總結了幾個C#經常使用的自界說函數,異常適用。分享給年夜家供年夜家參考。詳細以下:
1.將數組轉成字符串
/// <summary>
/// 將數組轉成字符串
/// </summary>
/// <param name="glue">分隔符</param>
/// <param name="pieces">要字符串數組</param>
private string Implode(char glue,string[] pieces)
{
string result = string.Empty;
int count = pieces.Length;
for (int i = 0; i < count;i++ )
{
if(i==0){
result = pieces[i];
}else{
result = result + glue.ToString() + pieces[i];
}
}
return result;
}
2.DateTime時光格局轉換為Unix時光戳格局
/// <summary>
/// DateTime時光格局轉換為Unix時光戳格局
/// </summary>
/// <param name=”time”></param>
/// <returns></returns>
private int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
3.生成某個規模內的隨機數
/// <summary>
/// 取得某個規模內的隨機數
/// </summary>
/// <param name="start">隨機數的下界</param>
/// <param name="end">隨機數的上界</param>
/// <returns>[minValue, maxValue)規模內的隨機整數</returns>
private int GetRandomInt(int minValue, int maxValue)
{
Random r = new Random(Chaos_GetRandomSeed());
return r.Next(minValue, maxValue);
}
/// <summary>
/// 加密隨機數生成器,生成隨機種子
/// </summary>
/// <returns></returns>
private static int Chaos_GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
願望本文所述對年夜家的C#法式設計有所贊助