类的扩展

自从博客园帐号被盗以后好长时间不写随便了,今天开始继续写写javascript

类的扩展.
js版
<script language="javascript">java

var a = "hello"
console.log(a);c#

String.prototype.HasValue = function () {
if (this == null || this == undefined || this.length == 0)
return false;
else
return true;
}this

console.log(a.HasValue());prototype

</script>ip

c#版
public static class StringExrprp
{
/// <summary>
/// 断定字符串是否有值
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool HasValue(this string str)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
return true;
}
}字符串

static void Main(string[] args)
{
string str = "test";
Console.WriteLine(str.HasValue());
//对比原始使用方法
Console.WriteLine(string.IsNullOrEmpty(str));
Console.ReadLine();
}博客

相关文章
相关标签/搜索