I am creating a basic class that has a user settable property that must be of type int
. How do I go about checking that the type passed in by the user is actually an integer and throwing an exception if it is of some other type?
class MyClass
{
private int _amount;
public int Amount
{
get
{
return _amount;
}
set
{
Type t = typeof(value);
if(t == typeof(int))
{
_amount = value;
}
else
{
throw new ArgumentException("Amount must be an integer", "Amount");
}
}
}
}
The Visual Studio IDE is saying The type or namespace value could not be found
. But I am using the type checking as specified in this SO question. I am using type so the check will take place at compile time.