This article mainly introduces C # Common application functions, combined with examples to summarize and analyze C # commonly used time, URL, HTML, reflection, fractional arithmetic and other related functions, the need for friends can refer to the following
This article summarizes the common application functions of C #. Share to everyone for your reference, as follows:
1, page write CS code (code embedded)
<%@ 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 = "Welcome login!"; } else { msg = "User not found"; } %><%= this.msg%>
2. Get time interval
<summary>///Get time interval (time interval for simulating Weibo posts)//</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 minutes ago"; } else if (TS. TotalHours < 1 && ts. Totalminutes > 0) { return Convert.ToInt32 (ts. totalminutes) + "minutes Ago"; } else if (TS. TotalHours < 4) { return Convert.ToInt32 (ts. TotalHours) + "hours ago"; } 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. Iterate through the list of parameters in the URL
<summary>///traverse the list of parameters in the URL///</summary>///<returns> such as: (? 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. Clear the text HTML code
Using system.text.regularexpressions;///<summary>///Clear Text HTML code//</summary>public string Removehtmltag (String htmlstr) { if (string. IsNullOrEmpty (HTMLSTR)) return string. Empty; Return Regex.Replace (Htmlstr, @ "<[^>]*>", "");}
5. Reflection creates class instances by class name
Using system.reflection;///<summary>///reflection creates a class instance through the class name///</summary>public void Reflectest () { Object Objclass = assembly.getexecutingassembly (). CreateInstance ("MyStudy.BLL.BookInfoBLL"); Parameter: The fully qualified name of the class, without the suffix name of the class 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 String.Format ("{0:c}", obj);}
7. Number of decimal places
1. Number of decimal places 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. Reserved n bits, rounded. Decimal d= decimal. Round (decimal. Parse ("0.55555"), 2);//3. Reserve N-bit rounding Math.Round (0.55555, 2);
8, using TryGetValue to improve access to the dictionary worth the performance
Use TryGetValue to increase the performance of a large number of values by a factor of containskey.
dictionary<int, string> dic = new Dictionary<int, string> ();d IC. ADD (1, "Zhang San");d IC. ADD (2, "John Doe"); string name = "";//error notation, efficiency bottom if (dic. ContainsKey (1)) {name = Dic[1]; Console.WriteLine (name);} Correct wording, increase efficiency by one-times if (DIC. TryGetValue (1, out name)) {Console.WriteLine (name);}