.NET(C#)代码性能优化

本文介绍了C#编程中的一些最佳实践,包括如何正确初始化空字符串、有效捕获异常及比较字符串的方法。这些技巧有助于提高代码质量和运行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

①string ax=string.Empty;     
注:不要使用: string ax="";  I don't konw why.But it's Microsoft's convention.
判断ax是否为空:
ax.Length==0   >  ax==string.Empty
    Info         : "To test for empty strings, check if String.Length is
                   equal to zero. Constructs such as "".Equals(someString)
                   and String.Empty.Equals(someString) are less efficient
                   than testing the string length. Replace these with
                   checks for someString.Length == 0."


②Catch{}要捕获详细异常,不要再乱抛
catch the specific exception than "System.Exception" or rethrow the exception.
Catching generic exception types can hide run-time problems from the library user,
and can complicate debugging.

③比较字符串时不要用.ToLower()   /   .ToUpper()
replace by Equals()     
Don't create a string never used later.
【待续】

转载于:https://www.cnblogs.com/kingjiong/archive/2009/01/20/1378969.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值