Framework是否提供可选参数

Framework是否提供可选参数

本文介绍了Compact Framework是否提供可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取名称符合特定格式的手持设备上所有文件的列表,例如" ABC .XML"

I need to get a list of all files on a handheld device whose names fit a certain pattern, such as "ABC.XML"

我从此处改编了代码(Hernaldo的答案),就像这样:

I adapted code from here (Hernaldo's answer), like so:

public static List<string> GetXMLFiles(string fileType, string dir)
{
    string dirName = dir; // call it like so: GetXMLFiles("ABC", "\\");  <= I think the double-whack is what I need for Windows CE device...am I right?
    var fileNames = new List<String>();
    try
    {
        foreach (string f in Directory.GetFiles(dirName))
        {
            if ((f.Contains(fileType)) &&  (f.Contains(".XML")))
            {
                fileNames.Add(f);
            }
        }
        foreach (string d in Directory.GetDirectories(dirName))
        {
            GetXMLFiles(fileType, d);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return fileNames;
}

...但是每次方法递归调用自身时(在GetDirectories()循环中),我都会传递相同的旧第一个arg.是否可以(在Compact Framework中)代替执行以下操作:

...but each time the method recursively calls itself (in the GetDirectories() loop), I'm passing the same old first arg. Is it possible (in Compact Framework) to do something like this instead:

public static List<string> GetXMLFiles(optional string fileType, string dir)
{
        . . .
        foreach (string d in Directory.GetDirectories(dirName))
        {
            GetXMLFiles(dir = d);
        }
        . . .

?

根据哈比卜(Habib)的说法,这应该有效(新的尝试"部分):

According to Habib, this should work (new "try" section):

try
{
    string filePattern = string.Format("*{0}*.XML", fileType);
    foreach (string f in Directory.GetFiles(dirName, filePattern))
    {
        fileNames.Add(f);
    }
    foreach (string d in Directory.GetDirectories(dirName))
    {
        GetXMLFiles(fileType, d);
    }
}

Ja?

这与我对艾伦(Alan)的以下评论的第二个答复:

This goes along with my second response to Alan's comment below:

const string EXTENSION = ".XML";
. . .
try
{
    foreach (string f in Directory.GetFiles(dirName))
    {
        string ext = Path.GetExtension(f);
        string fileNameOnly = Path.GetFileNameWithoutExtension(f);
        if ((ext.Equals(EXTENSION, StringComparison.Ordinal)) && (fileNameOnly.Contains(fileType)))
        {
            fileNames.Add(f);
        }
    }
    foreach (string d in Directory.GetDirectories(dirName))
    {
        GetXMLFiles(fileType, d);
    }
}

推荐答案

可选参数是.NET 4.0的功能,因此您将无法使用它们.使用递归函数的典型解决方案是为您的方法使用不同的参数创建两个重载.

Optional parameters are a feature of .NET 4.0, so you won't be able to use them. The typical solution to this with a recursive function is to create two overloads for your method with different parameters.

例如,

BinarySearch(array a);
{
  BinarySearch(a, -1, array.Length);
}

BinarySearch(array, low, high)
{
   //code to update low high
   return BinarySearch(array, low, high);
}

在这种情况下,开始递归的方法的签名稍有不同.您可以做同样的事情.

In this case, the method which kicks off the recursion has a slightly different signature. You could do the same.

这篇关于Compact Framework是否提供可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:31