This project is read-only.
1
Vote

Negative value can be assigned to ushort constant

description

Compile following code with B.dll reference
using System;
using TypeLib;

public class Bar
{
      public const ushort FIELD = Foo.CONSTANT;

      public static void Main ()
      {
        Console.WriteLine (FIELD);
        Console.WriteLine (FIELD == -1);
      }
}
Run it and it prints
-1
True

That's -1 for ushort constant value. There should probably be error check for metadata constant which have different Field type and Default value type

file attachments

comments

pgavlin wrote Jan 14, 2015 at 8:20 PM

This behavior is consistent with previous versions of the C# compiler and cannot be changed without breaking existing source code, so we have decided not to fix this issue.

AdamSpeight2008 wrote Jan 14, 2015 at 9:02 PM

An analyser could be used to warn of assign negative values to an unsigned type.