這篇文章主要介紹了C#常見應用函數,結合執行個體形式總結分析了C#常用的時間、URL、HTML、反射、小數運算等相關函數,需要的朋友可以參考下
本文執行個體總結了C#常見應用函數。分享給大家供大家參考,具體如下:
1、頁面寫CS代碼(代碼內嵌)
<%@ Import Namespace="System" %><%@ Import Namespace="System.Collections.Generic" %><Script runat="server"> public int userId = 0; protected void Page_Load(object sender, EventArgs e) { userId =Convert.ToInt32(Request.QueryString["UserID"]); Response.Write(userId); }</Script><% if (userId > 0){ msg = "歡迎登入!"; } else { msg = "未找到使用者"; }%><%= this.msg %>
2、擷取時間間隔
/// <summary>/// 擷取時間間隔(類比微博發布文章的時間間隔)/// </summary>/// <param name="date"></param>/// <returns></returns>public string GetDateStr(DateTime date){ if (date < DateTime.Now) { TimeSpan ts = DateTime.Now - date; if (ts.TotalHours < 1 && ts.TotalMinutes < 1) { return "1分鐘前"; } else if (ts.TotalHours < 1 && ts.TotalMinutes > 0) { return Convert.ToInt32(ts.TotalMinutes) + "分鐘前"; } else if (ts.TotalHours < 4) { return Convert.ToInt32(ts.TotalHours) + "小時前"; } else if (DateTime.Now.Date == date.Date) { return date.ToString("HH:mm"); } else { return date.ToString("yyyy-MM-dd"); } } return date.ToString("yyyy-MM-dd");}
3、遍曆Url中的參數列表
/// <summary>/// 遍曆Url中的參數列表/// </summary>/// <returns>如:(?userId=43&userType=2)</returns>public string GetUrlParam(){ string urlParam = ""; if (Request.QueryString.Count > 0) { urlParam = "?"; NameValueCollection keyVals = Request.QueryString; foreach (string key in keyVals.Keys) { urlParam += key + "=" + keyVals[key] + "&"; } urlParam = urlParam.Substring(0, urlParam.LastIndexOf('&')); } return urlParam;}
4、清除文本HTML碼
using System.Text.RegularExpressions;/// <summary>/// 清除文本HTML碼/// </summary>public string RemoveHtmlTag(string htmlStr){ if (string.IsNullOrEmpty(htmlStr)) return string.Empty; return Regex.Replace(htmlStr, @"<[^>]*>", "");}
5、反射 通過類名建立類執行個體
using System.Reflection;/// <summary>/// 反射 通過類名建立類執行個體/// </summary>public void ReflecTest(){ Object objClass = Assembly.GetExecutingAssembly().CreateInstance("MyStudy.BLL.BookInfoBLL"); //參數:類的完全限定名,無需類的尾碼名 if (objClass != null) { BookInfoBLL bll = (BookInfoBLL)objClass; }}
6、貨幣類型轉換
/// <summary>/// 貨幣/// </summary>/// <param name="obj"></param>/// <returns></returns>public static string ToMoney(object obj){ return String.Format("{0:C}", obj);}
7、小數點位元
//1.小數點位元string str1 = String.Format("{0:F1}", 56789); //result: 56789.0string str2 = String.Format("{0:F2}", 56789); //result: 56789.00string str3 = String.Format("{0:N1}", 56789); //result: 56,789.0string str4 = String.Format("{0:N2}", 56789); //result: 56,789.00string str5 = String.Format("{0:N3}", 56789); //result: 56,789.000string str6 = (56789 / 100.0).ToString("#.##"); //result: 567.89string str7 = (56789 / 100).ToString("#.##"); //result: 567//2.保留N位,四捨五入 .decimal d= decimal.Round(decimal.Parse("0.55555"),2);//3.保留N位四捨五入Math.Round(0.55555, 2);
8、使用TryGetValue改善擷取字典值得效能
使用TryGetValue在大量取值時效能比ContainsKey提高一倍。
Dictionary<int, String> dic = new Dictionary<int, String>();dic.Add(1,"張三");dic.Add(2,"李四");string name = "";//錯誤寫法,效率底if (dic.ContainsKey(1)){ name = dic[1]; Console.WriteLine(name);}//正確寫法,效率提高一倍if (dic.TryGetValue(1, out name)){ Console.WriteLine(name);}