0

Is there any possibility to switch warnigs off on an individual, site-specific (not project-related) level?

Background: most warnings are null reference warnings on code that is already tested. In the heap of those, some mor important warnings may get lost.

I know that I can suppress warnings in the file and in the project.

Globally suppress c# compiler warnings

How to suppress Possible Null Reference warnings

Suppress warning on solution level. Treat warning as error on solution level (where the Answer on the question is from 2013 and it does not seem to work any more, and it would not solve my problem, either, which is:

As I am working in a team, I cannot suppress this in the projects because some other people may not like to suppress the warnings.

Is there any possibility to switch warnigs off on an individual, site-specific (not project-related) level?

16
  • 2
    "I get this warning even in evidently safe code" - well, yes, maybe. But do you also get it in situations where it's actually helpful? Also, how much will your coworkers on the team thank you for adding warnings for them that you were never aware of when you check in code? Mar 30, 2022 at 13:37
  • 1
    You can always add a ! to things you know are not null - item!.field Mar 30, 2022 at 14:19
  • 1
    It's complaining because there is a situation where thing can go wrong. It's your job to correct it. (Do not ignore it, and leave it to your co-workers....)
    – Luuk
    Mar 30, 2022 at 15:21
  • 2
    Can you give a minimal reproducible example where this actually produces a warning "because the compiler is too stupid"?
    – Jon Skeet
    Mar 30, 2022 at 15:21
  • 2
    (And in general, I'd say the best approach is just to fix the warnings. Even if the code is already tested and not throwing any exceptions, fixing the warnings will usually make your code more self-describing, by accurately expressing whether or not it can accept/return null values.)
    – Jon Skeet
    Mar 31, 2022 at 15:41

1 Answer 1

0

try do this way

if(collection is not null)
{
foreach(var item in collection)  
{  
   if(item.field = "xyz")
  {
    ...
  }
}
}
1
  • not the same; collection is not null but item may be null...
    – SDG
    Mar 30, 2022 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.