When learning MVC, I learned how to write extension methods, which is very convenient to use.
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using System. Text;
Namespace MvcApp1.Content
{
// <% @ Import Namespace = "MvcApp1.Content" %>
Public static class MyHtmlHelper
{
Public static string RedLable (this HtmlHelper helper, string lbstr)
{
StringBuilder sb = new StringBuilder ();
Sb. Append ("<span style = 'color: red; '> ");
Sb. Append (lbstr );
Sb. Append ("</span> ");
Return sb. ToString ();
}
}
}
Below, use the above form to write my extension method
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Tools. Common
{
/// <Summary>
/// Public Data Optimization class
/// </Summary>
Public static class DataHelper
{
# Region returns string type data based on whether it is null or ""
/// <Summary>
/// Return string type data based on whether it is null or ""
/// </Summary>
/// <Param name = "obj"> object to obtain data </param>
/// <Param name = "defaultValue"> if it is null or "", the default value is returned. If it is not specified, the system returns "" </param>
/// <Returns> </returns>
Public static string IsNull (this object obj, params object [] defaultValue)
{
If (obj = null | "". Equals (obj. ToString (). Trim ()))
{
If (defaultValue. Length = 0 | defaultValue [0] = null) return "";
Else return defaultValue [0]. ToString ();
}
Else
{
Return obj. ToString ();
}
}
# Endregion
# Region returns long type data based on whether it is null or ""
/// <Summary>
/// Return long type data based on whether it is null or ""
/// </Summary>
/// <Param name = "obj"> object to obtain data </param>
/// <Param name = "defaultValue"> if it is null or "", the default value is returned. If it is not specified, long. MinValue is returned. </param>
/// <Returns> </returns>
Public static long IsNull (this object obj, params long [] defaultValue)
{
If (obj = null | "". Equals (obj. ToString (). Trim ()))
{
If (defaultValue. Length = 0) return long. MinValue;
Else return defaultValue [0];
}
Else
{
Return long. Parse (obj. ToString ());
}
}
# Endregion
# Region returns int type data based on whether it is null or ""
/// <Summary>
/// Return int type data based on whether it is null or ""
/// </Summary>
/// <Param name = "obj"> object to obtain data </param>
/// <Param name = "defaultValue"> if it is null or "", the default value is returned. If it is not specified, int. MinValue is returned. </param>
/// <Returns> </returns>
Public static int IsNull (this object obj, params int [] defaultValue)
{
If (obj = null | "". Equals (obj. ToString (). Trim ()))
{
If (defaultValue. Length = 0) return int. MinValue;
Else return defaultValue [0];
}
Else
{
Return int. Parse (obj. ToString ());
}
}
# Endregion
# Region returns DateTime type data based on whether it is null or ""
/// <Summary>
/// Return DateTime type data based on whether it is null or ""
/// </Summary>
/// <Param name = "obj"> object to obtain data </param>
/// <Param name = "defaultValue"> if it is null or "", the default value is returned. If it is not specified, DateTime. MinValue is returned. </param>
/// <Returns> </returns>
Public static DateTime IsNull (this object obj, params DateTime [] defaultValue)
{
If (obj = null | "". Equals (obj. ToString (). Trim ()))
{
If (defaultValue. Length = 0) return DateTime. MinValue;
Else return defaultValue [0];
}
Else
{
Return DateTime. Parse (obj. ToString ());
}
}
# Endregion
# Region returns double type data based on whether it is null or ""
/// <Summary>
/// Return double type data based on whether it is null or ""
/// </Summary>
/// <Param name = "obj"> object to obtain data </param>
/// <Param name = "defaultValue"> if it is null or "", the default value is returned. If it is not specified, double. MinValue is returned. </param>
/// <Returns> </returns>
Public static double IsNull (this object obj, params double [] defaultValue)
{
If (obj = null | "". Equals (obj. ToString (). Trim ()))
{
If (defaultValue. Length = 0) return double. MinValue;
Else return defaultValue [0];
}
Else
{
Return double. Parse (obj. ToString ());
}
}
# Endregion
# Region returns decimal type data based on whether it is null or ""
/// <Summary>
/// Return decimal type data based on whether it is null or ""
/// </Summary>
/// <Param name = "obj"> object to obtain data </param>
/// <Param name = "defaultValue"> default value returned when it is null or "". If it is not specified, decimal. MinValue is returned. </param>
/// <Returns> </returns>
Public static decimal IsNull (this object obj, params decimal [] defaultValue)
{
If (obj = null | "". Equals (obj. ToString (). Trim ()))
{
If (defaultValue. Length = 0) return decimal. MinValue;
Else return defaultValue [0];
}
Else
{
Return decimal. Parse (obj. ToString ());
}
}
# Endregion
}
}
Call method:
[Csharp]
Long L = "12312321". IsNull (long. MinValue );
Response. Write (L );
Result: