C # .NET: Is it possible to generate compile-time warnings when a property is set to invalid values?

It is just a matter of feasibility. I know that if I say

int myInt = "5"; 

I get a compile time error. I want to create compile-time errors or object warnings. So, let's say I have a custom object with several properties. One of the properties cannot be null, otherwise the solution will not compile:

  public static class NoNullObjects { //NotNullable public static NotNullObject {get; set;} } 

MyClass.cs:

  Line#55 NoNullObjects.NotNullObject = null; 

When I build, I want to see:

  Error: NotNullObject cannot be set to null. MyClass.cs Line 55. 

Is there any way to do this?

+4
source share
2 answers

No, not only with C #. Working with Microsoft Code Contracts can give you what you want: http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx .

+4
source

Here is a question that covers the same topic. The accepted answer assumes that you are using the contract code .

0
source

Source: https://habr.com/ru/post/1303156/


All Articles