0

I know it doesn't exist in C# as an operator but it's not an error. What does it mean?

using System;

namespace MyApp // Note: actual namespace depends on the project name.
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int a = 2;
            int b = 4;
            int c = 5;

            if (a + b!>c)
            {
                Console.WriteLine("abc");
            }
        }
    }
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
pythin
  • 29
  • 1
  • 2
    If this compiles, it's definitely not C# 4. It's telling the compiler that `b` isn't `null`. – CodeCaster May 19 '23 at 07:16
  • 1
    ! is logical negation, so negation of more than is less than... Btw, this is the first time that I see this kind of usage :D – j.v. May 19 '23 at 07:21
  • 1
    @j.v. `!` is the [null-forgiving operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving). – shingo May 19 '23 at 07:24
  • @shingo you are right, just tested... it's stupid though that is used on non-nullable types in this case, but it compiles... – j.v. May 19 '23 at 07:27
  • @j.v. If you try to run the code (which i know see you did) above `a + b! > c` comes out as true. Since there is already a negative version of both `>` and `<` this refers to a null-forgiving operator. And has (in this sample code) no added benefit as far as I know. It is just formatted in a non proper way to show what the `!` belongs to – Roe May 19 '23 at 07:28

0 Answers0