C#递归调用和数组

C#递归调用和数组,c#,arrays,C#,Arrays,我坐在这里,发现自己在为C编写递归调用,以编写一个注册表键 这是我可以很容易地硬编码的东西,但我想递归地做 using System; using System.Collections.Generic; using Microsoft.Win32; private const string regKeyPath = @"Software\Apps\jp2code\net\TestApp"; static void Main() { string[] split = regKeyPath.

我坐在这里,发现自己在为C编写递归调用,以编写一个
注册表键

这是我可以很容易地硬编码的东西,但我想递归地做

using System;
using System.Collections.Generic;
using Microsoft.Win32;

private const string regKeyPath = @"Software\Apps\jp2code\net\TestApp";

static void Main() {
  string[] split = regKeyPath.Split('\\');
  RegistryKey key = null;
  try {
    keyMaker(Registry.LocalMachine, split);
  } finally {
    if (key != null) {
      key.Close();
    }
  }
  // continue on with Application.Run(new Form1());
}
所以,
keyMaker
是我想要的递归函数

private static void keyMaker(RegistryKey key, string[] path) {
  string subKey = null;
  string[] subKeyNames = key.GetSubKeyNames();
  foreach (var item in subKeyNames) {
    if (path[0] == item) {
      subKey = item;
    }
  }
  RegistryKey key2 = null;
  try {
    if (String.IsNullOrEmpty(subKey)) {
      key2 = key.CreateSubKey(subKey);
    } else {
      key2 = key.OpenSubKey(subKey);
    }
    keyMaker(key2, &path[1]); // <= NOTE! Not allowed/defined in C#
  } finally {
    key2.Close();
  }
}
private static void keyMaker(注册表键,字符串[]路径){
字符串子键=null;
string[]subKeyNames=key.GetSubKeyNames();
foreach(子关键字名称中的变量项){
if(路径[0]==项){
子键=项;
}
}
RegistryKey key2=null;
试一试{
if(String.IsNullOrEmpty(子键)){
key2=key.CreateSubKey(子键);
}否则{
key2=key.OpenSubKey(子键);
}

keyMaker(key2,&path[1]);//更改方法签名以包含起始索引的简单方法:

void keyMaker(RegistryKey key, string[] path, int startIndex)
除此之外,您还可以使用或而不是数组,并使用
LinkedList.RemoveFirst()
Queue.Dequeue()
方法删除它们的头元素


但是您根本不需要递归来解决这个问题(除非这是一个练习)。

一个简单的方法是更改方法的签名以包含起始索引:

void keyMaker(RegistryKey key, string[] path, int startIndex)
除此之外,您还可以使用或而不是数组,并使用
LinkedList.RemoveFirst()
Queue.Dequeue()
方法删除它们的头元素


但您根本不需要递归来解决这个问题(除非这是一个练习)。

编辑以响应LOL

keyMaker(Registry.LocalMachine, ref split, 0);
....
private static void keyMaker(RegistryKey key, ref string[] path, int index) {
if( index > path.length - 1 ) return;
....
if (path[index] == item) {
....
keyMaker(key2, ref path, ++index);
....

编辑回应LOL

keyMaker(Registry.LocalMachine, ref split, 0);
....
private static void keyMaker(RegistryKey key, ref string[] path, int index) {
if( index > path.length - 1 ) return;
....
if (path[index] == item) {
....
keyMaker(key2, ref path, ++index);
....

不要递归地执行此操作。考虑到一个键仅由if返回,我将如何编写它:

private static void keyMaker(RegistryKey key, string[] path) {
    foreach(string subkey in path) {
        key = key.CreateSubKey(subkey);
    }
}
如果立即关闭它们很重要(我对此表示怀疑):


不要递归地执行此操作。考虑到一个键仅由if返回,我将如何编写它:

private static void keyMaker(RegistryKey key, string[] path) {
    foreach(string subkey in path) {
        key = key.CreateSubKey(subkey);
    }
}
如果立即关闭它们很重要(我对此表示怀疑):


虽然我更喜欢像@Groo建议的那样传递索引,但另一种可能是使用
IEnumerable
而不是
string[]
并使用LINQ。在递归调用中,可以传递
path.Skip(1)
,这将从列表中删除第一个元素(或者,更准确地说,返回一个从第二个元素开始的新的
IEnumerable

虽然我更喜欢像@Groo建议的那样传递索引,但另一种可能是使用
IEnumerable
而不是
字符串[]
并使用LINQ。在递归调用中,可以传递
path.Skip(1)
,它将从列表中删除第一个元素(或者更准确地说,返回一个从第二个元素开始的新的
IEnumerable

不确定它是否对您有益,但是有一个名为
ArraySegment
的类,您可以使用它来传递路径数组的其余部分(不分配新数组,并允许您返回原始数组)不确定它是否对您有利,但有一个名为
ArraySegment
的类,您可以使用该类传递路径数组的其余部分(不分配新数组,并允许您返回原始数组)LOL-您在调用
keyMaker
时使用了非法的
&path
,但我理解您所指的索引。您不需要使用
ref
。不管怎样都会传递引用。LOL-您在调用
keyMaker
时使用了非法的
&path
,但我理解您所指的索引。您不需要o使用
ref
。引用是无论如何传递的。我喜欢这样。这可能是我的方式,所以不要更改它。不过,我很好奇如何传递数组,给定一个数组。我喜欢这样。这可能是我的方式,所以不要更改它。不过,我很好奇如何传递数组,给定一个数组。这只是一个练习,因为我没有看到这样做的方法使用
string[]array
。我从来没有想过
LinkedList
Queue
。好主意!K.I.S.S.-我刚刚添加了索引值。这只是一个练习,因为我没有看到使用
string[]数组
。我从来没有想过
链接列表
队列
。好主意!K.I.S.S.-我刚刚添加了索引值。LINQ-我总是喜欢找个地方尝试一些!LINQ-我总是喜欢找个地方尝试一些!