C# 如何在C中的一个函数中传回字符串和布尔值?
所以我有这个函数C# 如何在C中的一个函数中传回字符串和布尔值?,c#,arrays,list,C#,Arrays,List,所以我有这个函数 public bool FileExists(string path, string filename) { string fullPath = Path.Combine(path, "pool"); string[] results = System.IO.Directory.GetFiles(fullPath, filename, SearchOption.AllDirectories); return (res
public bool FileExists(string path, string filename)
{
string fullPath = Path.Combine(path, "pool");
string[] results = System.IO.Directory.GetFiles(fullPath, filename, SearchOption.AllDirectories);
return (results.Length == 0 ? false : true);
}
它返回true或false关于是否在一个目录及其所有子目录中找到一个文件…但我也想传递字符串位置
我这样称呼它
if (FileExists(location, attr.link))
{
FileInfo f = new FileInfo("string the file was found");
关于如何实现这一点有什么想法吗?可能会更改为列表或数组…任何想法都可以添加所需的参数
bool FileExists(string path, string filename, out string somthElse)
{
somthElse = "asdf";
return true;
}
添加所需的参数
bool FileExists(string path, string filename, out string somthElse)
{
somthElse = "asdf";
return true;
}
将该方法重命名为TryFindFile并提供签名:
public bool TryFindFile(string path, string filename, out string location)
如果找到文件,该方法将返回true,并将location设置为location,否则将返回false,并将location设置为null。如果有多个位置,您可以将location键入字符串[]。将该方法重命名为TryFindFile,并提供签名:
public bool TryFindFile(string path, string filename, out string location)
如果找到文件,该方法将返回true,并将location设置为location,否则将返回false,并将location设置为null。如果有多个位置,您可以将位置键入字符串[]。您可以将输出参数out string[]结果传递给该方法并保留该方法,也可以更改该方法并返回结果数组,并在调用者中检查true或false
更便宜的方法是添加一个out参数。您可以将输出参数out string[]results传递给方法并保留该方法,也可以更改方法并返回结果数组,并在调用者中检查true或false
更便宜的方法是添加一个out参数。多种方法-返回带有两个字段的结构或类,使用关键字modified,使用a。多种方法-返回带有两个字段的结构或类,使用关键字modified,使用a。可以使用out参数吗
public bool FileExists(string path, string filename, out string location)
{
string fullPath = Path.Combine(path, "pool");
string[] results = System.IO.Directory.GetFiles(fullPath, filename, SearchOption.AllDirectories);
var doesExist = (results.Length == 0 ? false : true);
location = fullPath;//or whatever it is
}
你可以这样称呼它
if (FileExists(path, filename, out location))
{
//location now holds the path
}
可以找到有关out参数的更多信息。您可以使用out参数吗
public bool FileExists(string path, string filename, out string location)
{
string fullPath = Path.Combine(path, "pool");
string[] results = System.IO.Directory.GetFiles(fullPath, filename, SearchOption.AllDirectories);
var doesExist = (results.Length == 0 ? false : true);
location = fullPath;//or whatever it is
}
你可以这样称呼它
if (FileExists(path, filename, out location))
{
//location now holds the path
}
可以找到有关out参数的更多信息。您的意思是只希望返回文件所在位置的所有事件吗 您只需执行以下操作:
public static string[] GetFiles(string path, string filename)
{
string fullPath = Path.Combine(path, "pool");
return System.IO.Directory.GetFiles(fullPath, filename, SearchOption.AllDirectories);
}
这样使用:
var files = GetFiles(location, attr.link);
if (files.Any())
{
//Do stuff
}
您的意思是只想返回文件所在位置的所有事件吗 您只需执行以下操作:
public static string[] GetFiles(string path, string filename)
{
string fullPath = Path.Combine(path, "pool");
return System.IO.Directory.GetFiles(fullPath, filename, SearchOption.AllDirectories);
}
这样使用:
var files = GetFiles(location, attr.link);
if (files.Any())
{
//Do stuff
}
为匹配返回字符串还是为不匹配返回null有效?否则,您可以返回一个包含bool和字符串的简单结构,或者如果它接受一个out-type字符串参数以返回in中的值,但这两个参数都不是很好。您是否可能返回多个匹配的文件?返回匹配的字符串或返回不匹配的null是否有效?否则,您可以返回一个包含bool和字符串的简单结构,或者如果返回时接受out-type字符串参数以返回in中的值,但这两个参数都不是很好。您是否可能返回多个匹配的文件?+1。我喜欢这样,而且对于那些使用各种框架的人来说应该是很熟悉的尝试方法good point RichardOD,以及很好的答案Jason。这应该是答案,尽管Dave在下面指出该文件可以在多个地方找到。也许您的Jason的答案应该将var键入字符串[].+1。我喜欢这样,而且对于那些使用各种框架的人来说应该是很熟悉的尝试方法good point RichardOD,以及很好的答案Jason。这应该是答案,尽管Dave在下面指出该文件可以在多个地方找到。也许Jason的答案应该将var键入字符串[]。