AZ.*_*AZ. 1 c# exception-handling exception
说我有以下方法:
void UpdateContentMethodA(Int32[] src, Int32[] dst)
{
for(int i=0; i<src.Length; i++)
{
dst[i] = src[i];
}
}
void UpdateContentMethodB(Int32[] src, Int32[] dst)
{
if(src == null || dst == null) throw new System.ArgumentNullException();
if(src.Length != dst.Length) throw new System.IndexOutOfRangeException();
for(int i=0; i<src.Length; i++)
{
dst[i] = src[i];
}
}
Run Code Online (Sandbox Code Playgroud)
我是否写了methodB或者你认为methodA没问题?
编辑:对不起,我对编写这些方法很懒:
这些方法仅用于演示目的,真正的代码使用不同的数据类型,异常实例在其构造函数中传递有用的消息.
我了解到,优秀的程序员应该编写好的代码,即使它仅用于演示目的.你们真棒,谢谢!
我个人会将代码更改为:
void UpdateContentMethodB(Int32[] src, Int32[] dst)
{
if (src == null) throw new ArgumentNullException("src");
if (dst == null) throw new ArgumentNullException("dst");
if(src.Length != dst.Length) throw new ArgumentException("src and dst must be the same length", "src");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
148 次 |
最近记录: |