Menu
  • HOME
  • TAGS

Code Contracts: ensure constraint on delegate to avoid “requires unproven” warning?

c#,.net,delegates,code-contracts

f is not known to be pure. Calling it twice may give different results, so assuming that the first result is non-null says nothing about the second result. You should be able to store the result in a variable, add an assumption about that variable, and then pass that to...

Code Contract: malformed when incapsulate require method and unable to use String.Format

c#,code-contracts,malformed

For point #1 have a look here Contract overloads taking a userMessage such as Requires(bool condition, string userMessage) require that the userMessage message be a literal, or static (e.g. static readonly), or const, as per the error message. Since the user message is for you, the developer, not for users...

How can I indicate result ambiguity with Resharper CodeAnnotation attributes?

c#,resharper,code-contracts,resharper-8.0,resharper-7.1

You could use canbenull: [ContractAnnotation("null => null; notnull => canbenull")] The full grammar is: FDT ::= FDTRow [;FDTRow]* FDTRow ::= Input => Output | Output <= Input Input ::= ParameterName: Value [, Input]* Output ::= [ParameterName: Value]* {halt|stop|void|nothing|Value} Value ::= true | false | null | notnull | canbenull As...

Code contract invariant violation in C# using Entity Framework

c#,entity-framework,asp.net-mvc-5,code-contracts,object-oriented-analysis

You can create another class for data access (with EF) as repository and avoid this sort of contract errors; e.g: public class GenericRepository<T>: IDisposable where T : class { private SabaDataEntity db = null; private DbSet<T> table = null; static readonly GenericRepository<T> instance = new GenericRepository<T>(); public static readonly GenericRepository<T>...

Code Contracts for composed interfaces

c#,oop,inheritance,interface,code-contracts

The CodeContracts compile-time rewriter will automatically discover and use the contracts for all base interfaces. For your specific example (note how you do NOT need to repeat any of the base interfaces' contracts, and yet they still work): using System; using System.Diagnostics.Contracts; namespace Demo { [ContractClass(typeof(CanAddContract))] public interface ICanAdd {...

Class invariant for mutually exclusive conditions

c#,.net,code-contracts

It looks like simple OR should be enougth: Contract.Invariant(this.InstanceOfFoo == null || this.InstanceOfBar == null); Proof (for someone, who downvoted :) 1. (null, null): true || true -> true 2. (inst, null): false || true -> true 3. (null, inst): true || false -> true 4. (inst, inst): false ||...

Specify a parameter “may be null or not null” in Code Contracts?

c#,null,code-contracts

This can be done with Code Contracts, but not with Require. With require the checker always warns that it is redundant: even though the redundancy is the "correct" intent it is expressed incorrectly. Instead, Assert can be used as it is treated slightly differently and doesn't cause the warning. (However...

Operator '==' cannot be applied to operands of type 'Type?' and 'Type?'

c#,.net,code-contracts

You'll need to overload the == operator for that type if you expect to be able to use it to compare two instances (whether nullable or not) of that type.

Contract.Requires not preventing a null reference warning (purple squiggly)

c#,code-contracts

Dependent upon your project settings, I do not believe the static checker is always able to work out the invariants on origin and destination. There are several approaches which could be taken to resolve this warning: ContractInvariantMethod Add an explicit private method attributed with the ContractInvariantMethodAttribute and makes call to...

Handling the usual errors: If-Then-Throw blocks vs. Code Contracts vs. an Assert class

c#,exception,coding-style,assert,code-contracts

To be honest, I'm not sure a custom Assert class would help clarify anything, and I'm not sure you should be worried about two lines need to check and throw an exception vs one line. Your current way of checking parameters is the way we do things as well. In...

Contracts - How to Require collection contains no nulls

c#,code-contracts

The Assume will get of the message but I am not sure if this will lead to performance issues, also it seems redundant. Unfortunately I could not get rid of the static checker message without using Contract.Assume. // at the beginning of your method Contract.Requires(MyCollection != null && Contract.ForAll(MyCollection, x...

How to prove to CodeContracts that IEnumerable.Single() never returns null?

c#,code-contracts

In my understanding, Single() never returns null Not true - string[] a = new string[] {null}; bool check = (a.Single() == null); // true It returns either the exactly only value of the IEnumerable or it throws an exception. That is correct - so if the collection contains just...

CodeContracts fails the build for seemingly no reason

c#,.net,msbuild,code-contracts

The issue seems to be that it by default looks for contract types in Microsoft.Contracts (per spec) which doesn't work because Microsoft.Contracts is not what I am using. I'm using System.Diagnostics.Contracts, which is located in mscorlib. It doesn't tell me this, rather it does something non-sensical. I started looking through...

Does [Pure] have any implications other than “no visible side-effects” to Code Contracts?

c#,custom-attributes,code-contracts,purely-functional

The static analyser assumes that calling the same pure function with the same arguments twice in a row produces the same result. Given [Pure] public delegate int F(int i); public class A { public void f(F f) { var i = f(1); Contract.Assert(i == f(1)); } } a warning is...

Code Contracts and Tasks

c#,.net,async-await,task,code-contracts

Code contracts and async don't go together well, so you can't really use Contract.Ensures. There is however a workaround. You can change your method from a Task-returning method to an async one (which would be cleaner anyway) and use Contract.Assume instead: public async Task LoadAppModel() { var userTask = store.GetUserAsync();...

Code Contracts with no dlls on disk on asp.net 5/vnext

c#,code-contracts,roslyn,asp.net-5

The default behavior when building is as you say, but you do still have the option to create output files if you'd rather. If you're working at the command line, go to the location of your project.json file and use the kpm build command. This will generate Nuget packages as...

CodeContracts: Boolean condition evaluates to a constant value, why?

c#,code-contracts

This is simply a bug in Code Contracts. It is easy to concoct inputs that make this condition true or false. The warning is bogus. From personal experience I know that bugs in CC are not rare. How to fix? Since this is a bug there is no official/intended course...

How do I tell in c# codecontracts that a external method never returns null?

c#,code-contracts

The simple approach is: var nameIdentifier = myClaimsIdentity.FindFirst(ClaimTypes.NameIdentifier); Contract.Assume(nameIdentifier != null); nameIdentifier.Value; Code contracts will not try to prove the Assume condition, but will use it when proving other requirements. It's probably possible to create a contract reference assembly for the external code which has the appropriate Ensures post-conditions. The...

“Contract section within try block” error for context connection

c#,.net,entity-framework,code-contracts

Try calling another method, public string getLevelName() { using (var c = new myContext()) { return getLevelNameFrom(c); } } public string getLevelNameFrom(MyContext c) { Contract.Ensures(Contract.Result<string>() == c.Level.FirstOrDefault(i => i.levelId == this.levelId).name); return c.Level.FirstOrDefault(i => i.levelId == this.levelId).name; } ...