C # Write Extension Method

Source: Internet
Author: User

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:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.