VB.Net 中的递归函数示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8176993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:47:08  来源:igfitidea点击:

Recursive function examples in VB.Net

vb.netrecursion

提问by Keith Beard

I have seen other posts, but they are mostly in C#. For someone looking to learn recursion, seeing real world working examples in VB.Net could prove helpful. It's an added difficulty to try to decipher and convert C#if someone is just getting their feet wet programming in VB.Net. I did find this postwhich I understand now, but if there had been a post of VB.Net examples, I may have been able to pick it up faster.This is in part why I am asking this question:

我看过其他帖子,但大部分都是用 C# 写的。对于想要学习递归的人来说,在 VB.Net 中查看真实世界的工作示例可能会有所帮助。如果有人刚刚开始在 VB.Net 中进行编程,那么尝试破译和转换 C#是一个额外的困难。我确实找到了我现在理解的这篇文章,但是如果有一篇关于 VB.Net 示例的文章,我可能能够更快地找到它。这就是我问这个问题的部分原因:

Can anyone could show some simple examples of recursion functions in VB.Net?

任何人都可以在 VB.Net 中展示一些简单的递归函数示例吗?

采纳答案by CodingBarfield

This one from the wiki article is great

维基文章中的这篇很棒

Sub walkTree(ByVal directory As IO.DirectoryInfo, ByVal pattern As String)
 For Each file In directory.GetFiles(pattern)
    Console.WriteLine(file.FullName)
 Next
 For Each subDir In directory.GetDirectories
    walkTree(subDir, pattern)
 Next
End Sub

回答by adatapost

Take a look at MSDN article - Recursive Procedures (Visual Basic). This article will help you to understand the basics of recursion.

看看 MSDN 文章 -递归过程(Visual Basic)。本文将帮助您了解递归的基础知识。

Please refer the following links:

请参考以下链接:

  1. Recursive function to read a directory structure
  2. Recursion, why it's cool.
  1. 递归函数读取目录结构
  2. 递归,为什么它很酷。

回答by dbasnett

One of the classics

经典之一

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Try
        Label1.Text = Factorial(20).ToString("n0")
    Catch ex As Exception
        Debug.WriteLine("error")
    End Try
End Sub

Function Factorial(ByVal number As Long) As Long
    If number <= 1 Then
        Return (1)
    Else
        Return number * Factorial(number - 1)
    End If
End Function 'Factorial

In .Net 4.0 you could use BigInteger instead of Long...

在 .Net 4.0 中,您可以使用 BigInteger 而不是 Long ...

回答by mchar

This is a recursion example direct from msdn for VB.NET 2012:

这是一个直接来自 msdn 的递归示例,用于 VB.NET 2012:

Function factorial(ByVal n As Integer) As Integer
    If n <= 1 Then 
        Return 1
    Else 
        Return factorial(n - 1) * n
    End If 
End Function

Reference

参考