當使用代碼契約,我得到了警告:如何代碼契約使用的TryParse沒有得到警告
檢測調用方法 「System.Int32.TryParse(System.String,System.Int32 @)」沒有方法
具有與像下面的代碼的inteface定義的接口和代碼合同一類 合同[純]。問題是如何檢查字符串orgNumberWithoutControlDigit
可以轉換爲一個有效的整數,因爲它是模數工作的先決條件?
public string getControlDigit(string orgNumberWithoutControlDigit)
{
List<int> orgNumberNumbers = this.getNumberList(orgNumberWithoutControlDigit);
List<int> productList = orgNumberNumbers.Zip(this.weightNumberList, (first, second) => first * second).ToList();
int modular = productList.Sum() % 11;
string controlDigit = getControlDigit(modular);
return controlDigit;
}
private static string getControlDigit(int modular)
{
string controlDigit;
if (modular == 0)
{
controlDigit = "0";
}
else if (modular == 1)
{
controlDigit = "-";
}
else
{
int result = 11 - modular;
controlDigit = result.ToString();
}
return controlDigit;
}
[ContractClass(typeof(CalculateOrgNumberControlDigitBusinessContract))]
public interface ICalculateOrgNumberControlDigitBusiness
{
string getControlDigit(string orgNumberWithoutControlDigit);
}
[ContractClassFor(typeof(ICalculateOrgNumberControlDigitBusiness))]
public abstract class CalculateOrgNumberControlDigitBusinessContract:ICalculateOrgNumberControlDigitBusiness
{
public string getControlDigit(string orgNumberWithoutControlDigit)
{
Contract.Requires(orgNumberWithoutControlDigit.Length == 8);
int parseResult;
Contract.Requires(int.TryParse(orgNumberWithoutControlDigit, out parseResult));
Contract.Ensures(parseResult >= 0);
var result = Contract.Result<string>();
Contract.Ensures(result != null && result.Length == 1);
return default(string);
}
}
閱讀「純度」,地址爲:http://msdn.microsoft.com/en-us/library/dd264808.aspx –