gpt4 book ai didi

c# - c#中递归的返回类型

转载 作者:太空宇宙 更新时间:2023-11-03 20:07:15 24 4
gpt4 key购买 nike

我们应该在 c# 中使用哪种返回类型?是否可以在 c# 中使用“void”返回类型进行递归?

我有一个返回类型为 void 的函数,但是当我调用它时,它会递归地经历无限循环,所以有什么更好的解决方案:

我正在使用如下所述的功能:

void A()
{
//Some code
A()
}

最佳答案

我认为您对递归的理解需要加强。

仔细看看Recursion

Despite the usefulness of recursion, you can easily create a recursive function that never returns a result and cannot reach an endpoint. Such a recursion causes the computer to execute an infinite loop.

Another problem that can occur with recursion is that a recursive function can use all the available resources (such as system memory and stack space). Each time a recursive function calls itself (or calls another function that calls the original function), it uses some resources. These resources are freed when the recursive function exits, but a function that has too many levels of recursion may use all the available resources. When this happens, an exception is thrown.

您的问题不是返回类型。您需要有一些规则才能结束重生,比如说重生调用的深度,或者作为最大重现的目录数量。

或许也可以看看 Recursive methods using C#

关于c# - c#中递归的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22370531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com