Search code examples
c#.netout-parameters

Null out parameters in C#?


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.

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.


Solution

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