空出在C#中的参数? [英] Null out parameters in C#?

查看:118
本文介绍了空出在C#中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读计算器的经过检查,你应该使用DateTime.TryParse日期时间的格式的情况下。在尝试一些正则表达式EX pressions他们似乎得到长期和讨厌的覆盖地段的格式。

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.

不过的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.

所以我留下了持有出的结果,我什么都不做它一个变量。有没有一种方法,所以我没有做出来的参数?

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.

推荐答案

都能跟得上。我把它包在一个方法的地方,以保持噪声在主流的:

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()
  }

这篇关于空出在C#中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆