Null out parameters in C#?

后端 未结 6 1138
深忆病人
深忆病人 2021-01-17 11:19

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 l

6条回答
  •  逝去的感伤
    2021-01-17 11:59

    I'm not suggesting you actually do this, but you could use a single helper class to make this easy for all out parameters:

    public static class OutHelper
    {
        [ThreadStatic]
        public static T Ignored;
    }
    

    Then you can call:

    if (DateTime.TryParse(text, out OutHelper.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 :)

提交回复
热议问题