堆栈溢出异常

发布于 2024-08-15 17:23:56 字数 303 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

假扮的天使 2024-08-22 17:23:56

不要将空数组空数组混淆。您使用空数组再次调用相同的方法,但您必须停止此函数的唯一检查是检查空数组,并引发异常。你需要类似的东西:(

if (args.length == 0) {
   // bail out somehow
}

在空检查之后,以防止 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:

if (args.length == 0) {
   // bail out somehow
}

(after your null check, to prevent NPEs)

风吹短裙飘 2024-08-22 17:23:56

这是因为重载解析选择相同的 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.

旧时模样 2024-08-22 17:23:56

该函数将无限递归,因为没有终止条件:当使用非空 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.

耳钉梦 2024-08-22 17:23:56

也许你想要类似的东西

public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
{
  if (args == null) 
      return ExecuteNonQuery(procedure, new SqlParameter[] { });

  // Do some stuff here where args can be assumed != null
}

Perhaps you wanted something like

public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
{
  if (args == null) 
      return ExecuteNonQuery(procedure, new SqlParameter[] { });

  // Do some stuff here where args can be assumed != null
}
怎言笑 2024-08-22 17:23:56

你的 args 变量永远不会为空;您应该编码:

return ExecuteNonQuery(procedure, null);

或创建一个没有 args 参数的重载。

Your args variable is never null; You should to code:

return ExecuteNonQuery(procedure, null);

or to create an overload which doesn't have an args argument.

单挑你×的.吻 2024-08-22 17:23:56

您需要检查列表的长度是否为 0,而不是是否为空。或者你可以将其传递为 null,如下所示:

 public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
 {
            if (args == null) throw new ArgumentNullException("args");
            else
            return ExecuteNonQuery(procedure, 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:

 public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
 {
            if (args == null) throw new ArgumentNullException("args");
            else
            return ExecuteNonQuery(procedure, null);
 }
向日葵 2024-08-22 17:23:56

因为您在 return 语句中调用了自己。

Because you call yourself in the return statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文