C# 字符串数组的初始化

发布于 2024-12-25 00:41:28 字数 593 浏览 2 评论 0原文

我想声明一个字符串数组,我使用的是这种方法:

string[] matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);

效果很好。

现在我想将 Directory.GetFiles 调用包含在 try/catch 块中,但我也无法声明字符串数组在那里,因为这样它就不会在 try 块之外使用它的正确范围内。但如果我尝试这样做:

string[] matchingActiveLogFiles;
    try
    {
        matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
    }
    catch (Exception ex)
    {
        //log error
    }
            

我尚未初始化字符串数组,因此出现错误。所以我想知道在这种情况下的最佳实践是什么,我应该在 try 块之外声明字符串数组吗?如果是的话怎么办?

I want to declare a string array, I was using this way of doing it:

string[] matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);

which worked perfectly.

Now I want to enclose the Directory.GetFiles call in a try/catch block, but I can't also have the declaration of the string array in there because then it won't be in the right scope to use it outside of the try block. But if I try this:

string[] matchingActiveLogFiles;
    try
    {
        matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
    }
    catch (Exception ex)
    {
        //log error
    }
            

I have not initialized the string array so I have an error. So I am wondering what is best practise in this situation, should I declare the string array outside the try block? And if so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

残龙傲雪 2025-01-01 00:41:29

字符串数组的名称不同,一个是 matchingActiveLogFiles,另一个是 matchingFiles

string[] matchingActiveLogFiles;
    try
    {
        matchingActiveLogFiles = Directory.GetFiles(FilePath, FileNamePattern);
    }
    catch (Exception ex)
    {
        //log error
    }

The name is different for the string arrays, one is matchingActiveLogFiles the other is matchingFiles:

string[] matchingActiveLogFiles;
    try
    {
        matchingActiveLogFiles = Directory.GetFiles(FilePath, FileNamePattern);
    }
    catch (Exception ex)
    {
        //log error
    }
猥琐帝 2025-01-01 00:41:29

这将初始化您的数组:

string[] matchingActiveLogFiles = {};
    try
    {
        matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
    }
    catch (Exception ex)
    {
        //log error
    }

但我想知道,您遇到了什么错误?

即使使用未初始化的数组,上面的代码也应该可以工作。

我还注意到您在第 1 行有“matchingActiveLogFiles”,在第 4 行有“matchingFiles”。也许这就是您的问题?

This will initialize your array:

string[] matchingActiveLogFiles = {};
    try
    {
        matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
    }
    catch (Exception ex)
    {
        //log error
    }

But I'm wondering, what error are you getting?

Even with an uninitialized array, the above code should work.

I also noticed that you have "matchingActiveLogFiles" on line 1 and "matchingFiles" on line 4. Perhaps that's your problem?

还在原地等你 2025-01-01 00:41:29

问题是你的命名。您正在定义matchingActiveLogFiles,但分配matchingFiles。

The issue is your naming. You're defining matchingActiveLogFiles but assigning matchingFiles.

甜点 2025-01-01 00:41:29

您应该在需要该变量的范围内声明该变量。

  • 如果您只需要 try 块中的变量,请将其放在那里!
  • 如果您在 try 块之外需要它,那么如果您的代码无法获取文件内容,您希望该值是什么?出错时将其设置为该值。

You should declare the variable in the scope in which that variable is needed.

  • If you only need the variable in the try block, put it in there!
  • If you need it outside of the try block, what do you want the value to be if your code can't get the file contents? Set it to that on error.
最好是你 2025-01-01 00:41:29

您不必知道项目的确切数量即可初始化数组,例如

 string[] matchingActiveLogFiles = {}; 
this is what is called a dynamic array it's totally functional and I've had ZERO issues with declaring arrays this way.. 

List<string> matchingFiles= new List<string>();
try
 {
    matchingFiles.Add(Directory.GetFiles(FilePath, FileNamePattern));
    matchingActiveLogFiles = matchingFiles.ToArray(); 
 }
 catch (Exception ex)
 {
    //logerror
 }  

you do not have to know the exact number of Items in order to initialize an array like

 string[] matchingActiveLogFiles = {}; 
this is what is called a dynamic array it's totally functional and I've had ZERO issues with declaring arrays this way.. 

List<string> matchingFiles= new List<string>();
try
 {
    matchingFiles.Add(Directory.GetFiles(FilePath, FileNamePattern));
    matchingActiveLogFiles = matchingFiles.ToArray(); 
 }
 catch (Exception ex)
 {
    //logerror
 }  
李不 2025-01-01 00:41:29

首先初始化它:

 string[] matchingActiveLogFiles = new string[0];

Initialize it first:

 string[] matchingActiveLogFiles = new string[0];
唠甜嗑 2025-01-01 00:41:29

虽然我通常不喜欢没有参数的方法,但这似乎是 Try 方法的一个很好的候选者:

bool TryGetMatchingLogFiles(out string[] matchingFiles )
{
  matchingFiles = null;
  try
  {
     matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
     return true;
  }
  catch (Exception ex)
  {
     //logerror
     return false;
  }
}

用法:

string[] matchingActiveLogFiles;
if (TryGetMatchingLogFiles(out matchingActiveLogFiles))
{
  // Use matchingActiveLogFiles here
}

或者,只需将变量初始化为 null:

string[] matchingActiveLogFiles = null;
try ...

Though I generally dislike methods with out params, this seems like a good candidate for a Try method:

bool TryGetMatchingLogFiles(out string[] matchingFiles )
{
  matchingFiles = null;
  try
  {
     matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
     return true;
  }
  catch (Exception ex)
  {
     //logerror
     return false;
  }
}

Usage:

string[] matchingActiveLogFiles;
if (TryGetMatchingLogFiles(out matchingActiveLogFiles))
{
  // Use matchingActiveLogFiles here
}

Or alternatively, just initialise your variable to null:

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