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...
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...
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...
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>...
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 {...
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 ||...
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...
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.
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...
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...
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...
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...
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...
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...
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();...
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...
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...
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...
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; } ...