Reputation: 20271
I have an object that needs a test if the object data is valid. The validation itself would be called from the thread that instatiated the object, it looks like this:
{
if (_step.Equals(string.Empty)) return false;
if (_type.Equals(string.Empty)) return false;
if (_setup.Equals(string.Empty)) return false;
return true;
}
Would it be better to implement this as a property, or as a method, and why? I have read the answers to a related question, but I don't think this specific question is covered there.
Upvotes: 2
Views: 286
Reputation: 545518
That code needs refactoring. This is how you write code in Java, not in C#. In C#, you've got operator overloading.
if (_step == "")) return false;
if (_type == "")) return false;
if (_setup == "")) return false;
This is the idiomatic way of doing the comparison. Your way, besides being more verbose, is just unexpected and inconsistent in C#.
If, and only if, there's a chance that these strings are actually null
instead of empty, use the following instead:
if (string.IsNullOrEmpty(_step)) return false;
Upvotes: 1
Reputation: 45761
My personal opinion here would be:
Upvotes: 7
Reputation: 1701
I would say as a Property.
if(something.IsValid) { ...
looks better then
if(something.IsValid()) { ...
Also an example from MSDN: http://msdn.microsoft.com/en-us/library/system.web.ui.page.isvalid(VS.71).aspx
Upvotes: 1