C#常用自定义函数小结
程序员文章站
2023-12-16 16:55:22
本文实例总结了几个c#常用的自定义函数,非常实用。分享给大家供大家参考。具体如下:
1.将数组转成字符串
///
/// 将数...
本文实例总结了几个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#程序设计有所帮助