堆栈溢出异常
public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
{
if (args == null) throw new ArgumentNullException("args");
else
return ExecuteNonQuery(procedure, new SqlParameter[] { });
}
为什么在调用上述方法时获得递归函数并抛出 StackOverFlow 异常。(当参数包含 5 个值时)
public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
{
if (args == null) throw new ArgumentNullException("args");
else
return ExecuteNonQuery(procedure, new SqlParameter[] { });
}
Why getting recursive function and throwing StackOverFlow Exception when calling this above method.(while the argument contains 5 values)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不要将空数组与空数组混淆。您使用空数组再次调用相同的方法,但您必须停止此函数的唯一检查是检查空数组,并引发异常。你需要类似的东西:(
在空检查之后,以防止 NPE)
Don't confuse an empty array with a null array. You're calling the same method again with an empty array, but the only check you have to cease this function is to check for a null array, and throw an exception. You need something like:
(after your null check, to prevent NPEs)
这是因为重载解析选择相同的 ExecuteNonQuery 方法,因此您本质上是一遍又一遍地调用相同的方法。
您的方法采用 SqlParameter[] (参数部分只是语法糖),并且您再次调用相同的方法,并使用 SqlParameter[] 作为第二个参数。
That is because overload resolution picks the same ExecuteNonQuery method, so you are essentially calling the same method over and over again.
Your method takes a SqlParameter[] (the params part is just syntactic sugar), and you are calling the same method again, with a SqlParameter[] as the second argument.
该函数将无限递归,因为没有终止条件:当使用非空 args 参数调用时,它只是使用相同的参数调用自身,除了用完堆栈空间。
The function will recurse infinitely because there is no termination condition: when called with a non-null
args
parameter it just calls itself with indentical parameters, and there is nothing that stops this infinite recursion other than running out of stack space.也许你想要类似的东西
Perhaps you wanted something like
你的 args 变量永远不会为空;您应该编码:
或创建一个没有
args
参数的重载。Your
args
variable is never null; You should to code:or to create an overload which doesn't have an
args
argument.您需要检查列表的长度是否为 0,而不是是否为空。或者你可以将其传递为 null,如下所示:
you need to check if the list has a length of 0, not if it is null. Or you could pass it null, like so:
因为您在
return
语句中调用了自己。Because you call yourself in the
return
statement.