argument-validation

How to avoid argument validation

若如初见. 提交于 2019-12-06 03:51:38
问题 Validating Primitive Arguments and "Complex Data" Validating Arguments When writing a method, arguments should be validated first before any operations are performed. For example, let's say we've got a class representing people: public class Person { public readonly string Name; public readonly int Age; public class Person(string name, int age) { this.Name = name; this.Age = age; } } What's wrong with this Person class? name and age aren't validated before their values are set as fields of

What is the best practice in case one argument is null?

[亡魂溺海] 提交于 2019-12-03 08:44:28
问题 when validating methods' input, I used to check if the argument is null, and if so I throw an ArgumentNullException. I do this for each and every argument in the list so I end up with code like this: public User CreateUser(string userName, string password, string Email, string emailAlerts, string channelDescription) { if (string.IsNullOrEmpty(userName)) throw new ArgumentNullException("Username can't be null"); if (string.IsNullOrEmpty(Email)) throw new ArgumentNullException("Email can't be

What is the best practice in case one argument is null?

↘锁芯ラ 提交于 2019-12-02 22:38:30
when validating methods' input, I used to check if the argument is null, and if so I throw an ArgumentNullException. I do this for each and every argument in the list so I end up with code like this: public User CreateUser(string userName, string password, string Email, string emailAlerts, string channelDescription) { if (string.IsNullOrEmpty(userName)) throw new ArgumentNullException("Username can't be null"); if (string.IsNullOrEmpty(Email)) throw new ArgumentNullException("Email can't be null"); //etc, etc, etc } Is this OK? Why should I do this? Would it be ok if I simply group all the

C#: Best practice for validating “this” argument in extension methods

江枫思渺然 提交于 2019-11-30 20:48:32
Let's say I have an extension method public static T TakeRandom<T>(this IEnumerable<T> e) { ... To validate the argument e, should I: A) if (e == null) throw new NullReferenceException() B) if (e == null) throw new ArgumentNullException("e") C) Not check e What's the consensus? My first thought is to always validate arguments, so thrown an ArgumentNullException. Then again, since TakeRandom() becomes a method of e, perhaps it should be a NullReferenceException. But if it's a NullReferenceException, if I try to use a member of e inside TakeRandom(), NullReferenceException will be thrown anyway.

C#: Best practice for validating “this” argument in extension methods

最后都变了- 提交于 2019-11-30 04:55:26
问题 Let's say I have an extension method public static T TakeRandom<T>(this IEnumerable<T> e) { ... To validate the argument e, should I: A) if (e == null) throw new NullReferenceException() B) if (e == null) throw new ArgumentNullException("e") C) Not check e What's the consensus? My first thought is to always validate arguments, so thrown an ArgumentNullException. Then again, since TakeRandom() becomes a method of e, perhaps it should be a NullReferenceException. But if it's a