How do I use Code Contracts to have a compile time assert in C#? -


according this answer c# has "code contracts" should usable instead of c++ compile time asserts. have magic code:

intptr pointer; //blahblahblah pointer = new intptr(pointer.toint32() + marshal.sizeof(typeof(somestruct))); 

that requires intptr of same size int32. want compile time assert - c++ code

static_assert(sizeof(intptr)==sizeof(int32)) 

so tried following:

system.diagnostics.contracts.contract.assert(false); //just test pointer = new intptr(pointer.toint32() + marshal.sizeof(typeof(somestruct))); 

i pass false assert() surely fails, compilation passes fine.

so how use code contracts have compile time assert?

that because code contracts not same compile time asserts. still runtime code come static analysis rule set can enable in projects looking for.

take @ question looks answers issue well: contract.assert not throw compilation error


Comments