There is no such syntax. There is a proposal for it to enter C#, and I hope very much to enter.
Today there is a contract library which does something similar, but is quite inferior. The use of contracts has existed since the . NET 4.0.
The use of it along with static analysis tools can be powerful, and indeed helped well. I don’t know why but the newer versions of Visual Studio no longer have the tools and the mechanism has become less useful, unless you do a lot of manual things or on your own.
Doesn’t mean he hasn’t used it yet, but he’s not much different than a if
, at most gets more semantic. Anyway to be a first class mechanism need to be in the language and the compiler collaborate.
Contracts can be more interesting than typical mechanisms because they can be resolved at compile time and may even be deleted from the code becoming at no cost of execution time. Implemented in the right way can become part of the contract type and method, that is, whoever calls it must respect the contract directly. And in sophisticated mechanisms this can propagate and until only the input of the data is verified if it is within the contract eliminating all other checks because the data is already valid (is ideal, it is not simple to make it work in the language).
I like its use, but I admit that the current state of it on . NET is not so advantageous as it is, but also not so bad to avoid it. At least it gives more semantics and you can easily change later if the mechanism is improved.
I’m still trying to figure out why they are no longer emphasized. But the . NET code uses a lot, I think everyone should use more. Help give more robustness and readability.
Has wikipedia article on general use.
I answered something about them on Is there any functionality similar to Assert (affirmations) in C#?.
In theory then, should it ever be implemented, it would help in performance above just replacing some
if
within the method beyond readability?– Victor Laio
In some cases, yes. Today you can do this with external tools, it can rewrite the code, but these tools have not evolved anymore and are no longer in VS.
– Maniero
All right! Thank you!
– Victor Laio