Shouldn't using FieldInfo.SetValue to set a ValueType to null fail?

(related to PropertyInfo SetValue and nulls)

If I have public class Thing { public int X; }, a Thing o, and a FieldInfo fi that points to the X field, why is it legal to call fi.SetValue(o, null)? The runtime sets the field X to zero, i.e. default(int) instead of complaining that a ValueType cannot be set to null.

Does anyone know the design choice behind this behavior, which at least from C# violates my principle of least astonishment?

6
задан Community 23 May 2017 в 11:52
поделиться