I\'m attempting to resolve the following exercise:
You need to create a class named
Product
that represents a product. The class has a
If you want different exceptions on null and on empty string (often null means that something is totally wrong, when empty string is just a format error):
public string Name {
get {
return name;
}
set {
if (null == value)
throw new AgrumentNullException("value");
else if (String.Equals(value, ""))
throw new AgrumentException("Empty values are not allowed.", "value");
name = value;
}
}
In case you don't want to distiguish them:
public string Name {
get {
return name;
}
set {
if (String.IsNullOrEmpty(value))
throw new AgrumentException("Null or empty values are not allowed.", "value");
name = value;
}
}
Note, that in both cases it's value
that you have to test, not a property Name
. In your original code the name
's (and so Name
as well) initial value is null
and you'll get exception whatever you try to set.