.NET_Framework

.NET Core.NET Framework Acronym Glossary.NET Framework ADO.NET.NET Framework CLR.NET Framework Code Contracts.NET Framework Collections.NET Framework Custom Types.NET Framework DateTime parsing.NET Framework Dependency Injection.NET Framework Dictionaries.NET Framework Encryption / Cryptography.NET Framework Exceptions.NET Framework Expression Trees.NET Framework File Input/Output.NET Framework ForEach.NET Framework Garbage Collection.NET Framework Globalization in ASP.NET MVC using Smart internationalization for ASP.NET.NET Framework HTTP clients.NET Framework HTTP servers.NET Framework Introduction.NET Framework JIT compiler.NET Framework JSON Serialization.NET Framework LINQ.NET Framework Managed Extensibility.NET Framework Memory management.NET Framework Networking.NET Framework NuGet packaging system.NET Framework Platform Invoke.NET Framework Process and Thread affinity setting.NET Framework Reading and writing Zip files.NET Framework ReadOnlyCollections.NET Framework Reflection.NET Framework Regular Expressions (System.Text.RegularExpressions).NET Framework Serial Ports.NET Framework Settings.NET Framework SpeechRecognitionEngine class to recognize speech.NET Framework Stack and Heap.NET Framework Strings.NET Framework Synchronization Contexts.NET Framework System.Diagnostics.NET Framework System.IO.NET Framework System.IO.File class.NET Framework System.Net.Mail.NET Framework System.Reflection.Emit namespace.NET Framework System.Runtime.Caching.MemoryCache (ObjectCache).NET Framework Task Parallel Library (TPL).NET Framework Task Parallel Library (TPL) API Overviews.NET Framework Threading.NET Framework TPL Dataflow.NET Framework Unit testing.NET Framework Upload file and POST data to webserver.NET Framework Using ProgressT and IProgressT.NET Framework VB Forms.NET Framework Work with SHA1 in C Sharp.NET Framework Write to and read from StdErr stream.NET Framework XmlSerializerJSON in .NET with Newtonsoft.JsonParallel processing using .NET framework



.NET Framework Code Contracts

From WikiOD

Remarks[edit | edit source]

Code contracts allow for compile or runtime analysis of pre/post conditions of methods and invariant conditions for objects. These conditions may be used to ensure callers and return value match valid states for application processing. Other uses for Code Contracts include documentation generation.

Contracts for Interfaces[edit | edit source]

Using Code Contracts it is possible to apply a contract to an interface. This is done by declaring an abstract class that implments the interfaces. The interface should be tagged with the ContractClassAttribute and the contract definition (the abstract class) should be tagged with the ContractClassForAttribute

C# Example...

[ContractClass(typeof(MyInterfaceContract))]
public interface IMyInterface
{
    string DoWork(string input);
}
//Never inherit from this contract defintion class
[ContractClassFor(typeof(IMyInterface))]
internal abstract class MyInterfaceContract : IMyInterface
{
    private MyInterfaceContract() { }

    public string DoWork(string input)
    {
        Contract.Requires(!string.IsNullOrEmpty(input));
        Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()));
        throw new NotSupportedException();
    }
}
public class MyInterfaceImplmentation : IMyInterface
{
    public string DoWork(string input)
    {
        return input;
    }
}

Static Analysis Result...

Code_Contracts

Preconditions[edit | edit source]

Preconditions allows methods to provide minimum required values for input parameters

Example...

void DoWork(string input)
{
    Contract.Requires(!string.IsNullOrEmpty(input));

    //do work
}

Static Analysis Result...

Code_Contracts

Postconditions[edit | edit source]

Postconditions ensure that the returned results from a method will match the provided definition. This provides the caller with a definition of the expected result. Postconditions may allowed for simplied implmentations as some possible outcomes can be provided by the static analyizer.

Example...

string GetValue()
{
    Contract.Ensures(Contract.Result<string>() != null);

    return null;
}

Static Analyis Result...

Code_Contracts

Installing and Enabling Code Contracts[edit | edit source]

While System.Diagnostics.Contracts is included within the .Net Framework. To use Code Contracts you must install the Visual Studio extensions.

Under Extensions and Updates search for Code Contracts then install the Code Contracts Tools

Code Contract Tools install

After the tools are installed you must enable Code Contracts within your Project solution. At the minimum you probably want to enable the Static Checking (check after build). If you are implementing a library that will be used by other solutions you may want to consider also enabling Runtime Checking.

Project Settings

Credit:Stack_Overflow_Documentation