在 C# 中清空参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1414469/
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
Null out parameters in C#?
提问by chobo2
After reading on stackoverflow that in the case of checking the format of a DateTime you should use DateTime.TryParse. After trying some regex expressions they seem to get long and nasty looking to cover lots of the formatting.
在 stackoverflow 上阅读后,在检查 DateTime 格式的情况下,您应该使用 DateTime.TryParse。在尝试了一些正则表达式之后,他们似乎变得冗长而讨厌,希望涵盖许多格式。
But TryParse requires an "out" parameter and since I just want to do a validation format check I don't need the actual result.
但是 TryParse 需要一个“out”参数,因为我只想进行验证格式检查,所以我不需要实际结果。
So I am left with a variable that holds the "out" result and am to do nothing with it. Is there a way so I don't have to do a out parameter?
所以我留下了一个保存“输出”结果的变量,我什么都不用做。有没有办法让我不必做输出参数?
So I get rid of this warning and stop having a variable just flying around.
所以我摆脱了这个警告并停止让变量四处乱飞。
采纳答案by Kim Gr?sman
Nope. I'd wrap it in a method somewhere to keep the noise out of the main flow:
不。我会将它包装在某个地方的方法中,以防止噪音进入主流:
bool IsValidDate(string value)
{
DateTime result;
return DateTime.TryParse(value, out result); //result is stored, but you only care about the return value of TryParse()
}
回答by Mehrdad Afshari
No. You can't get rid of the variable but you shouldn't get a compiler warning either.
不。您无法摆脱变量,但也不应该收到编译器警告。
Passing a variable as out
is "using" the variable. The compiler will not issue a warning because of that.
按原样out
“使用”变量传递变量。编译器不会因此发出警告。
回答by Pete OHanlon
If you are using .NET 3 and above, you could always create an Extension method?
如果您使用 .NET 3 及更高版本,您总是可以创建扩展方法吗?
public static bool IsValidDate(this string value)
{
DateTime date = DateTime.Null;
return DateTime.TryParse(value, out date);
}
[Edited to rename the method name to a more appropriate one]
[编辑以将方法名称重命名为更合适的名称]
回答by deostroll
TryParse
is a better option. Its just a variable that is wasted. Other options include using the Convert.ToDateTime()
within a try-catch block. But again that would not be efficient because try-catch blocks are meant to be heavy. The next option is regex. This is a better solution. I guess this gives you the result instantly than compared to the others.
TryParse
是更好的选择。它只是一个被浪费的变量。其他选项包括Convert.ToDateTime()
在 try-catch 块中使用 。但这同样效率不高,因为 try-catch 块很重。下一个选项是正则表达式。这是一个更好的解决方案。我想这会立即为您提供与其他人相比的结果。
You can very well wrap the method like Kim Gr?sman said...
您可以像 Kim Gr?sman 所说的那样很好地包装方法......
回答by Jon Skeet
I'm not suggesting you actually dothis, but you coulduse a single helper class to make this easy for allout parameters:
我并不是建议您实际执行此操作,但是您可以使用单个帮助程序类来简化所有输出参数的操作:
public static class OutHelper<T>
{
[ThreadStatic]
public static T Ignored;
}
Then you can call:
然后你可以调用:
if (DateTime.TryParse(text, out OutHelper<DateTime>.Ignored))
It's horrible, uses a public mutable field, and if your application is also executing with some malicious code, it gives that code access to the last value you've parsed... but it should work :)
太可怕了,使用公共可变字段,并且如果您的应用程序还使用一些恶意代码执行,它会允许该代码访问您解析的最后一个值......但它应该可以工作:)
回答by jeubank12
With C#7.0 (since August 2016) you can use the out var construct, and then just ignore the new var in subsequent code.
使用 C#7.0(自 2016 年 8 月起),您可以使用 out var 构造,然后在后续代码中忽略新的 var。
bool success = DateTime.TryParse(value, out var result);
If you truly do not care about the value of the result, use discards:
如果您真的不关心结果的价值,请使用discards:
bool success = DateTime.TryParse(value, out _);