???????????????衣???validator??????????е?CASE??????
public class Validator
{
public string ErrorMsg { get; private set; }
public bool Validate(string input)
{
if (string.IsNullOrEmpty(input))
{
ErrorMsg = "the input can't be empty.";
return false;
}
if (input.Length != 4)
{
ErrorMsg = "the input must be four digits.";
return false;
}
var regex = new Regex(@"^[0-9]*$");
if (!regex.IsMatch(input))
{
ErrorMsg = "the input must be fully digital.";
return false;
}
if (input.Distinct().Count() != 4)
{
ErrorMsg = "the input figures can't contain duplicate.";
return false;
}
return true;
}
}
Run...

???????CASE????????IF???????2??CASE????????"^d{4}$"?Cover"4λ????"??????????????????????
????С????????????ú?С?????????????????????????????????????????????????????????????С??????????ж??????ν"??????????????????????С????????"????????????????????????????á?
?????????IF?????????????в????????????????????????IF????????????????????????Щ??
public class Validator
{
public string ErrorMsg { get; private set; }
public bool Validate(string input)
{
return IsEmpty(input) && IsFourdigits(input) && IsDigital(input) && IsRepeat(input);
}
private bool IsEmpty(string input)
{
if (!string.IsNullOrEmpty(input))
{
return true;
}
ErrorMsg = "the input can't be empty.";
return false;
}
private bool IsFourdigits(string input)
{
if (input.Length == 4)
{
return true;
}
ErrorMsg = "the input must be four digits.";
return false;
}
private bool IsDigital(string input)
{
var regex = new Regex(@"^[0-9]*$");
if (regex.IsMatch(input))
{
return true;
}
ErrorMsg = "the input must be fully digital.";
return false;
}
private bool IsRepeat(string input)
{
if (input.Distinct().Count() == 4)
{
return true;
}
ErrorMsg = "the input figures can't contain duplicate.";
return false;
}
}