SlideShare a Scribd company logo
1 of 39
CODE CONTRACT
Larry Nung
AGENDA
Introduction
Design By Contract(DbC)
Code Contract
Contracts
Preconditions
Postconditions
Object Invariants
Contract.Assert & Contract.Assume
Contract.ForAll & Contract.Exists
Interface & Abstract Method Contracts
Contract Reference Assembly
Reference
Q & A
INTRODUCTION
Design By Contract(DbC)
 要求軟體設計者為軟體組件定義正式的,精
確的並且可驗證的介面。
 明確描述雙方的權利與義務。
 降低程式的錯誤發生率。
 提高程式的品質。
提升自
動測試
程度
CODE CONTRACT
提升自
動測試
程度
靜態驗
證
CODE CONTRACT
提升自
動測試
程度
靜態驗
證
執行階
段驗證
CODE CONTRACT
提升自
動測試
程度
靜態驗
證
執行階
段驗證
文件產
生
CODE CONTRACT
CODE CONTRACT
 可以透過設定啟用/關閉
 Intellisense提示強化
CODE CONTRACT
 Framewrok
 4.0+
 Namespace
 System.Diagnostics.Contracts
 Assemble
CODE CONTRACT
CODE CONTRACT
void Test(List<object> array, int index, object value)
{
Contract.Requires(index >= 0);
Contract.Ensures(array.Count == Contract.OldValue(array.Count) + 1);
array.Insert(index, value);
}
CODE CONTRACT
private void Test(List<object> array, int index, object value){
List<object> Contract.Old(array);
int Contract.Old(Count); __ContractsRuntime.Requires(index >= 0, null, "index >= 0");
try {
Contract.Old(array) = array;
} catch (Exception exception1) {
if (exception1 == null) { throw; }
}
try {
Contract.Old(Count) = array.Count;
} catch (Exception exception2) {
if (exception2 == null) { throw; }
}
array.Insert(index, value);
if (__ContractsRuntime.insideContractEvaluation <= 4) {
try {
__ContractsRuntime.insideContractEvaluation++;
__ContractsRuntime.Ensures(Contract.Old(array).Count == (Contract.Old(Count) + 1), null, "array.Count ==
Contract.OldValue(array.Count) + 1");
} finally {
__ContractsRuntime.insideContractEvaluation--;
}
}
}
CONTRACTS
PRECONDITIONS
 用來驗證進入方法或是屬性時所必須滿足的需求條件
 使用Contract.Requires方法表示
Contract.Requires(x != null );
Contract.Requires<ArgumentNullException>(x != null, "x
is null");
if(x == null) throw new ArguementNullException();
Contract.EndContractBlock();
POSTCONDITIONS
 用來驗證方法或屬性中止時所需滿足的需求條件
 標準後置條件
Contract.Ensures( this .F > 0 );
 例外後置條件
Contract.EnsuresOnThrow<T>( this.F > 0 );
POSTCONDITIONS
 特殊後置條件(方法傳回值、前置狀態值、與輸出參
數)
Contract.Ensures(0 < Contract.Result<int>());
void IArray.Insert(int index, Object value)
{
Contract.Requires(index >= 0);
Contract.Requires(index <= ((IArray)this).Count);
Contract.Ensures(((IArray)this).Count ==
Contract.OldValue(((IArray)this).Count) + 1);
}
POSTCONDITIONS
 前置狀態值不得參考方法的傳回值與用傳址方式帶
入的參數
Contract.OldValue(Contract.Result<int>() + x) //
ERROR
 如數量詞範圍相依於方法傳回值,前置狀態值不得
相依於數量詞的繫結變數
Contract. ForAll (0,Contract. Result<int>(),i =>
Contract.OldValue(xs[i]) > 3 ); // ERROR
POSTCONDITIONS
 除 ForAll 或 Exists 外,前置狀態值不得出現在匿名委派
的主體中
Method( ... (T t) => Contract.OldValue(... t ...) ... ); //
ERROR
 前置狀態值除非是當方法呼叫的索引子或引數使用,否則
不得在 ForAll 或 Exists 呼叫中參考匿名委派的參數
Contract. ForAll (0, xs .Length, i => Contract.OldValue(xs[i])
> 3); // OK
Contract. ForAll (0, xs .Length, i => Contract.OldValue(i) >
3 ); // ERROR
OBJECT INVARIANTS
 用來驗證總是需要滿足的不變條件
 使用ContractInvariantMethodAttribute 屬性搭配
Contract .Invariant去做驗證
public int MyProperty { get; set; }
…
[ContractInvariantMethod]
void ObjectInvariant()
{
Contract.Invariant(MyProperty >= 0);
}
OBJECT INVARIANTS
OBJECT INVARIANTS
public int MyProperty
{
[CompilerGenerated]
get
{
int Contract.Result = this.<MyProperty>k__BackingField;
if (!this.$evaluatingInvariant$)
{
__ContractsRuntime.Ensures(Contract.Result >= 0, null, "MyProperty >= 0");
}
return Contract.Result;
}
[CompilerGenerated]
set
{
if (!this.$evaluatingInvariant$)
{
__ContractsRuntime.Requires(value >= 0, null, "MyProperty >= 0");
}
this.<MyProperty>k__BackingField = value;
if (!this.$evaluatingInvariant$) { }
this.$InvariantMethod$();
}
}
CONTRACT.ASSERT & CONTRACT.ASSUME
Contract.Assert(this.privateField > 0);
Contract.Assert(this.x == 3, "Why isn't the value of x
3?");
CONTRACT.ASSERT & CONTRACT.ASSUME
Contract.Assume(this.privateField > 0);
Contract.Assume(this.x == 3, "Static checker
assumed this");
CONTRACT.FORALL & CONTRACT.EXISTS
public int Foo<T>(IEnumerable<T> xs)
{
Contract.Requires(Contract.ForAll (xs , x => x !=
null) );
}
public int Foo<T>(T[] xs)
{
Contract. Requires ( Contract.ForAll
(0,xs.Length,index => xs[index] != null));
}
CONTRACT.FORALL & CONTRACT.EXISTS
public int Foo<T>(IEnumerable<T> xs)
{
Contract.Requires(Contract.Exists(xs , x => x !=
null) );
}
INTERFACE & ABSTRACT METHOD
CONTRACTS
INTERFACE & ABSTRACT METHOD
CONTRACTS
[ContractClass(typeof(IFooContract))]
interface IFoo {
int Count { get; }
void Put(int value);
}
[ContractClassFor(typeof(IFoo))]
abstract class IFooContract : IFoo {
int IFoo.Count {
get {
Contract.Ensures(0 <= Contract.Result<int>());
return default(int); // dummy return
}
}
void IFoo.Put(int value) {
Contract.Requires(0 <= value);
}
}
INTERFACE & ABSTRACT METHOD
CONTRACTS
[ContractClass(typeof(FooContract))]
abstract class Foo {
public abstract int Count { get; }
public abstract void Put(int value);
}
[ContractClassFor(typeof(Foo))]
abstract class FooContract : Foo {
public override int Count {
get {
Contract.Ensures(0 <= Contract.Result<int>());
return default(int); // dummy return
}
}
public override void Put(int value) {
Contract.Requires(0 <= value);
}
}
CONTRACT REFERENCE ASSEMBLY
CONTRACT REFERENCE ASSEMBLY
CONTRACT REFERENCE ASSEMBLY
CONTRACT REFERENCE ASSEMBLY
REFERENCE
REFERENCE
 Code Contracts (1) - 概念與簡介
 https://msdn.microsoft.com/zh-tw/library/dn606159.aspx
 Code Contracts (2) - 三大合約
 https://msdn.microsoft.com/zh-tw/library/dn606157.aspx
 Code Contracts (3) - Contract.Assert &
Contract.Assume
 https://msdn.microsoft.com/zh-tw/library/dn606156.aspx
REFERENCE
 Code Contracts (4) - Contract.ForAll &
Contract.Exists
 https://msdn.microsoft.com/zh-tw/library/dn606166.aspx
 Code Contracts (5) - 介面合約與抽象方法合約
 https://msdn.microsoft.com/zh-tw/library/dn722305.aspx
 Code Contracts (6) - Contract Reference Assembly
 https://msdn.microsoft.com/zh-tw/library/dn722306.aspx
Q&A
37
QUESTION & ANSWER
38
THE END

More Related Content

Similar to Code contract

Use Geth to Deploy Contract
Use Geth to Deploy ContractUse Geth to Deploy Contract
Use Geth to Deploy ContractKC Tam
 
Serverless microservices: Test smarter, not harder
Serverless microservices: Test smarter, not harderServerless microservices: Test smarter, not harder
Serverless microservices: Test smarter, not harderDiUS
 
Workshop: .NET Code Contracts
Workshop: .NET Code ContractsWorkshop: .NET Code Contracts
Workshop: .NET Code ContractsRainer Stropek
 
Blockchain technology-in-fin tech - Anton Sitnikov
Blockchain technology-in-fin tech - Anton SitnikovBlockchain technology-in-fin tech - Anton Sitnikov
Blockchain technology-in-fin tech - Anton SitnikovDataFest Tbilisi
 
Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013David McCarter
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)Koen Metsu
 
Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe
Learn Cloud-Native .NET: Core Configuration Fundamentals with SteeltoeLearn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe
Learn Cloud-Native .NET: Core Configuration Fundamentals with SteeltoeVMware Tanzu
 
Software engineering ⊇ Software testing
Software engineering ⊇ Software testingSoftware engineering ⊇ Software testing
Software engineering ⊇ Software testingPavel Tcholakov
 
ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionDzmitry Ivashutsin
 
Contract-oriented PLSQL Programming
Contract-oriented PLSQL ProgrammingContract-oriented PLSQL Programming
Contract-oriented PLSQL ProgrammingJohn Beresniewicz
 
MongoDB.local Atlanta: Introduction to Serverless MongoDB
MongoDB.local Atlanta: Introduction to Serverless MongoDBMongoDB.local Atlanta: Introduction to Serverless MongoDB
MongoDB.local Atlanta: Introduction to Serverless MongoDBMongoDB
 
Use Geth to Access a Deployed Contract
Use Geth to Access a Deployed ContractUse Geth to Access a Deployed Contract
Use Geth to Access a Deployed ContractKC Tam
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)Will Huang
 
[Test bash manchester] contract testing in practice
[Test bash manchester] contract testing in practice[Test bash manchester] contract testing in practice
[Test bash manchester] contract testing in practicePierre Vincent
 

Similar to Code contract (20)

Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
Code Contracts
Code ContractsCode Contracts
Code Contracts
 
Use Geth to Deploy Contract
Use Geth to Deploy ContractUse Geth to Deploy Contract
Use Geth to Deploy Contract
 
Serverless microservices: Test smarter, not harder
Serverless microservices: Test smarter, not harderServerless microservices: Test smarter, not harder
Serverless microservices: Test smarter, not harder
 
Rhino Mocks
Rhino MocksRhino Mocks
Rhino Mocks
 
Coding Naked 2023
Coding Naked 2023Coding Naked 2023
Coding Naked 2023
 
Workshop: .NET Code Contracts
Workshop: .NET Code ContractsWorkshop: .NET Code Contracts
Workshop: .NET Code Contracts
 
Code Contracts API In .NET
Code Contracts API In .NETCode Contracts API In .NET
Code Contracts API In .NET
 
Blockchain technology-in-fin tech - Anton Sitnikov
Blockchain technology-in-fin tech - Anton SitnikovBlockchain technology-in-fin tech - Anton Sitnikov
Blockchain technology-in-fin tech - Anton Sitnikov
 
Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)
 
Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe
Learn Cloud-Native .NET: Core Configuration Fundamentals with SteeltoeLearn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe
Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe
 
Software engineering ⊇ Software testing
Software engineering ⊇ Software testingSoftware engineering ⊇ Software testing
Software engineering ⊇ Software testing
 
ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency Injection
 
Contract-oriented PLSQL Programming
Contract-oriented PLSQL ProgrammingContract-oriented PLSQL Programming
Contract-oriented PLSQL Programming
 
MongoDB.local Atlanta: Introduction to Serverless MongoDB
MongoDB.local Atlanta: Introduction to Serverless MongoDBMongoDB.local Atlanta: Introduction to Serverless MongoDB
MongoDB.local Atlanta: Introduction to Serverless MongoDB
 
Use Geth to Access a Deployed Contract
Use Geth to Access a Deployed ContractUse Geth to Access a Deployed Contract
Use Geth to Access a Deployed Contract
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
 
[Test bash manchester] contract testing in practice
[Test bash manchester] contract testing in practice[Test bash manchester] contract testing in practice
[Test bash manchester] contract testing in practice
 
Good code
Good codeGood code
Good code
 

More from Larry Nung

Ansible - simple it automation
Ansible - simple it automationAnsible - simple it automation
Ansible - simple it automationLarry Nung
 
sonarwhal - a linting tool for the web
sonarwhal - a linting tool for the websonarwhal - a linting tool for the web
sonarwhal - a linting tool for the webLarry Nung
 
LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLarry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8Larry Nung
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatLarry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7Larry Nung
 
BenchmarkDotNet - Powerful .NET library for benchmarking
BenchmarkDotNet  - Powerful .NET library for benchmarkingBenchmarkDotNet  - Powerful .NET library for benchmarking
BenchmarkDotNet - Powerful .NET library for benchmarkingLarry Nung
 
PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6Larry Nung
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualityLarry Nung
 
Visual studio 2017
Visual studio 2017Visual studio 2017
Visual studio 2017Larry Nung
 
Web deploy command line
Web deploy command lineWeb deploy command line
Web deploy command lineLarry Nung
 
Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Larry Nung
 
Common.logging
Common.loggingCommon.logging
Common.loggingLarry Nung
 
protobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETprotobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETLarry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5Larry Nung
 
Regular expression
Regular expressionRegular expression
Regular expressionLarry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4Larry Nung
 
Fx.configuration
Fx.configurationFx.configuration
Fx.configurationLarry Nung
 

More from Larry Nung (20)

Ansible - simple it automation
Ansible - simple it automationAnsible - simple it automation
Ansible - simple it automation
 
sonarwhal - a linting tool for the web
sonarwhal - a linting tool for the websonarwhal - a linting tool for the web
sonarwhal - a linting tool for the web
 
LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data file
 
PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization format
 
PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7
 
BenchmarkDotNet - Powerful .NET library for benchmarking
BenchmarkDotNet  - Powerful .NET library for benchmarkingBenchmarkDotNet  - Powerful .NET library for benchmarking
BenchmarkDotNet - Powerful .NET library for benchmarking
 
PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code Quality
 
Visual studio 2017
Visual studio 2017Visual studio 2017
Visual studio 2017
 
Web deploy command line
Web deploy command lineWeb deploy command line
Web deploy command line
 
Web deploy
Web deployWeb deploy
Web deploy
 
SikuliX
SikuliXSikuliX
SikuliX
 
Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...
 
Common.logging
Common.loggingCommon.logging
Common.logging
 
protobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETprotobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NET
 
PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5
 
Regular expression
Regular expressionRegular expression
Regular expression
 
PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4
 
Fx.configuration
Fx.configurationFx.configuration
Fx.configuration
 

Code contract

Editor's Notes

  1. 要求軟體設計者為軟體組件定義正式的,精確的並且可驗證的介面 核心思想是對軟體系統中的元素之間相互合作以及「責任」與「義務」的比喻 - 供應商必須提供某種產品(責任),並且他有權期望客戶已經付款(權利)。 - 客戶必須付款(責任),並且有權得到產品(權利)。 套用在程式中 - 期望所有調用它的客戶模塊都保證一定的進入條件:這就是函數的先驗條件—客戶的義務和供應商的權利,這樣它就不用去處理不滿足先驗條件的情況。 - 保證退出時給出特定的屬性:這就是函數的後驗條件—供應商的義務,顯然也是客戶的權利。 在進入時假定,並在退出時保持一些特定的屬性:不變式。 DbC同時也定義了軟體模塊的正確性條件: 如果對一個供應商的調用之前類的不變式和先驗條件是真,那麼在調用後不變式和後驗條件也為真。 當調用供應商時,軟體模塊應保證不違反供應商的先驗條件。
  2. Feature 可以很容易的為程式碼加入驗證程式碼,降低程式的錯誤發生率,提高程式的品質,也可以整合單元測試,減少單元測試的工作量,甚至整合文件產生器,讓產出的程式文檔更為詳細。 靜態驗證 靜態驗證功能只在專業版的Code Contract才支援。該功能能在編譯階段檢查隱含合約及明確合約,可判斷合約違規與否,甚至進一步給予建議。 執行階段驗證 執行階段驗證顧名思義該功能可提供運行階段下的檢查,可依據程式中撰寫的明確合約,判斷是否存在任何合約違規。   這邊值得注意的是,若在屬性頁中未勾選Assert On Contract Failure選項,執行階段驗證失敗時會觸發例外。而若有勾選該選項,執行階段驗證失敗時則會彈出斷言錯誤對話框。 文件產生 在文件產生方面,跟自動測試功能相似,Code Contract的設定可以提供文件產生器一些額外的資訊,讓支援Code Contract的文件產生器能透過Code Contract所提供的合約資訊,產生較為詳細的文件,像是Sandcastle就是支援Code Contract的文件產生器。   
  3. Feature 可以很容易的為程式碼加入驗證程式碼,降低程式的錯誤發生率,提高程式的品質,也可以整合單元測試,減少單元測試的工作量,甚至整合文件產生器,讓產出的程式文檔更為詳細。 靜態驗證 靜態驗證功能只在專業版的Code Contract才支援。該功能能在編譯階段檢查隱含合約及明確合約,可判斷合約違規與否,甚至進一步給予建議。 執行階段驗證 執行階段驗證顧名思義該功能可提供運行階段下的檢查,可依據程式中撰寫的明確合約,判斷是否存在任何合約違規。   這邊值得注意的是,若在屬性頁中未勾選Assert On Contract Failure選項,執行階段驗證失敗時會觸發例外。而若有勾選該選項,執行階段驗證失敗時則會彈出斷言錯誤對話框。 文件產生 在文件產生方面,跟自動測試功能相似,Code Contract的設定可以提供文件產生器一些額外的資訊,讓支援Code Contract的文件產生器能透過Code Contract所提供的合約資訊,產生較為詳細的文件,像是Sandcastle就是支援Code Contract的文件產生器。   
  4. Feature 可以很容易的為程式碼加入驗證程式碼,降低程式的錯誤發生率,提高程式的品質,也可以整合單元測試,減少單元測試的工作量,甚至整合文件產生器,讓產出的程式文檔更為詳細。 靜態驗證 靜態驗證功能只在專業版的Code Contract才支援。該功能能在編譯階段檢查隱含合約及明確合約,可判斷合約違規與否,甚至進一步給予建議。 執行階段驗證 執行階段驗證顧名思義該功能可提供運行階段下的檢查,可依據程式中撰寫的明確合約,判斷是否存在任何合約違規。   這邊值得注意的是,若在屬性頁中未勾選Assert On Contract Failure選項,執行階段驗證失敗時會觸發例外。而若有勾選該選項,執行階段驗證失敗時則會彈出斷言錯誤對話框。 文件產生 在文件產生方面,跟自動測試功能相似,Code Contract的設定可以提供文件產生器一些額外的資訊,讓支援Code Contract的文件產生器能透過Code Contract所提供的合約資訊,產生較為詳細的文件,像是Sandcastle就是支援Code Contract的文件產生器。   
  5. Feature 可以很容易的為程式碼加入驗證程式碼,降低程式的錯誤發生率,提高程式的品質,也可以整合單元測試,減少單元測試的工作量,甚至整合文件產生器,讓產出的程式文檔更為詳細。 靜態驗證 靜態驗證功能只在專業版的Code Contract才支援。該功能能在編譯階段檢查隱含合約及明確合約,可判斷合約違規與否,甚至進一步給予建議。 執行階段驗證 執行階段驗證顧名思義該功能可提供運行階段下的檢查,可依據程式中撰寫的明確合約,判斷是否存在任何合約違規。   這邊值得注意的是,若在屬性頁中未勾選Assert On Contract Failure選項,執行階段驗證失敗時會觸發例外。而若有勾選該選項,執行階段驗證失敗時則會彈出斷言錯誤對話框。 文件產生 在文件產生方面,跟自動測試功能相似,Code Contract的設定可以提供文件產生器一些額外的資訊,讓支援Code Contract的文件產生器能透過Code Contract所提供的合約資訊,產生較為詳細的文件,像是Sandcastle就是支援Code Contract的文件產生器。   
  6. 標準後置條件是用來驗證方法或屬性正常中止時所需滿足的需求條件 例外後置條件是用來驗證方法或屬性在擲回特定例外狀況時所需滿足的需求條件 特殊後置條件則是指方法傳回值、前置狀態值、與輸出參數這三種輔助後置條件用的語法
  7. 標準後置條件是用來驗證方法或屬性正常中止時所需滿足的需求條件 例外後置條件是用來驗證方法或屬性在擲回特定例外狀況時所需滿足的需求條件 特殊後置條件則是指方法傳回值、前置狀態值、與輸出參數這三種輔助後置條件用的語法
  8. 物件非變異(Object Invariants) 物件非變異是用來描述類別執行個體總是需要滿足的不變條件,使用上只要在方法上附加ContractInvariantMethodAttribute 屬性標記,並在方法中使用Contract .Invariant方法做一連串的驗證即可。值得注意的是該方法內只能包含Invariant契约,不得包含任何其它程式碼。
  9. 介面合約主要功用為為實作介面的類別提供統一的驗證合約,當我們為介面定義好了介面合約以後,所有實作該介面的類別都會享有到合約驗證的好處,不需每個類別各自撰寫,可減少撰寫重覆的驗證合約程式、增加程式中合約驗證覆蓋完整度、與加快實現合約式編程。 方法回傳值須用default(T)替代回傳