类的扩展

自从博客园账号被盗之后好长时间不写随便了,今天开始继续写写随便.
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();
}


明显js的类的扩展用着更方便,c#类的扩展不知道是谁设计的,本来c#并不支持类扩展,
但是看其他语言都开始支持所以也支持了,为了不改变原来的string类,所以把string当成一个参数来扩展string类,
这样的设计太无聊.

猜你喜欢

转载自www.cnblogs.com/frog2008/p/11608489.html
今日推荐