0
Nullable<int> a;
int b = 2;
a = b;

a is nullable integer. b is NON-nullable integer.

I want to receive warning or error in Visual Studio when a NON-nullable integer is assigned to a nullable integer [a = b]. Is it possible?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Sean C
  • 1
  • 5
    Why should that be a warning or error? It's an operation that is always safe. – Damien_The_Unbeliever Oct 12 '22 at 08:28
  • 1
    I don't know of any such warning and I doubt that it will ever exist, because it doesn't make much sense. If you really feel so inclined however, you could write your [own analyzer](https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix) for that. – Good Night Nerd Pride Oct 12 '22 at 08:31
  • Unless your nullable integer variable is always going to be null, then at some point it must be possible to assign a non-nullable integer value to it - so it does not make sense for it to be an error condition. – PaulF Oct 12 '22 at 08:37
  • this link has answer [custom-compiler-warnings](https://stackoverflow.com/questions/154109/custom-compiler-warnings) – Pradeep Kumar Oct 12 '22 at 09:03

1 Answers1

0

Try this

#warning "NON-nullable integer is assigned to a nullable integer."

For more details refer the below link

custom-compiler-warnings

Pradeep Kumar
  • 1,193
  • 1
  • 9
  • 21