类的扩展

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

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

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

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

console.log(a.HasValue());

</script>

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();
}



posted on 2019-09-29 16:16  xinshenghu  阅读(135)  评论(0编辑  收藏  举报