This article summarizes common C# application functions. Share it for your reference, as follows:
1. Write CS code on the page (code embedded)
<%@ Import Namespace="System" %> <%@ Import Namespace="" %> <Script runat="server"> public int userId = 0; protected void Page_Load(object sender, EventArgs e) { userId =Convert.ToInt32(["UserID"]); (userId); } </Script> <% if (userId > 0){ msg = "Welcome to log in!"; } else { msg = "User not found"; } %> <%= %>
2. Obtain the time interval
/// <summary> /// Get the time interval (simulate the time interval for posting on Weibo)/// </summary> /// <param name="date"></param> /// <returns></returns> public string GetDateStr(DateTime date) { if (date < ) { TimeSpan ts = - date; if ( < 1 && < 1) { return "1 minute ago"; } else if ( < 1 && > 0) { return Convert.ToInt32() + "Minutes ago"; } else if ( < 4) { return Convert.ToInt32() + "Hour ago"; } else if ( == ) { return ("HH:mm"); } else { return ("yyyy-MM-dd"); } } return ("yyyy-MM-dd"); }
3. Iterate through the parameter list in Url
/// <summary> /// traverse the parameter list in Url/// </summary> /// <returns>For example:(?userId=43&userType=2)</returns>public string GetUrlParam() { string urlParam = ""; if ( > 0) { urlParam = "?"; NameValueCollection keyVals = ; foreach (string key in ) { urlParam += key + "=" + keyVals[key] + "&"; } urlParam = (0, ('&')); } return urlParam; }
4. Clear text HTML code
using ; /// <summary> /// Clear text HTML code/// </summary> public string RemoveHtmlTag(string htmlStr) { if ((htmlStr)) return ; return (htmlStr, @"<[^>]*>", ""); }
5. Reflection Create class instances through class names
using ; /// <summary> /// Reflection Create class instances through class name/// </summary> public void ReflecTest() { Object objClass = ().CreateInstance(""); // Parameters: Fully qualified name of the class, no class suffix name is required if (objClass != null) { BookInfoBLL bll = (BookInfoBLL)objClass; } }
6. Currency type conversion
/// <summary> /// Currency/// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ToMoney(object obj) { return ("{0:C}", obj); }
7. Number of decimal points
//1. Number of decimal pointsstring str1 = ("{0:F1}", 56789); //result: 56789.0 string str2 = ("{0:F2}", 56789); //result: 56789.00 string str3 = ("{0:N1}", 56789); //result: 56,789.0 string str4 = ("{0:N2}", 56789); //result: 56,789.00 string str5 = ("{0:N3}", 56789); //result: 56,789.000 string str6 = (56789 / 100.0).ToString("#.##"); //result: 567.89 string str7 = (56789 / 100).ToString("#.##"); //result: 567 //2. Keep N bits and round.decimal d= (("0.55555"),2); //3. Keep N-bit rounding(0.55555, 2);
8. Use TryGetValue to improve the performance of obtaining dictionary value
Using TryGetValue doubles performance over ContainsKey when taking large amounts of values.
Dictionary<int, String> dic = new Dictionary<int, String>(); (1,"Zhang San"); (2,"Li Si"); string name = ""; //Wrong writing method, efficiencyif ((1)) { name = dic[1]; (name); } //Regular writing method, double the efficiencyif ((1, out name)) { (name); }
For more information about C# related content, please check out the topic of this site:Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.