谢谢你们这么有趣! :-) -SA [END EDIT]By the way, even considering such an off-the-scale amount of absurdities, I think it as an opportunity to learn something real.Thank you for so much fun! :-)—SA Console.WriteLine("Zero = {0}", default(int));Console.WriteLine("Zero = {0}", default(DateTime));Console.WriteLine("Zero = {0}", default(string)); 如果你坚持你的零名称,您可以将其包装到类中,例如If you insist in your Zero name, you might wrap it into a class, e.g. public abstract class Zero{ public static T Value<T>() { return default(T); }}...Console.WriteLine("Zero = {0}", Zero.Value<int>());Console.WriteLine("Zero = {0}", Zero.Value<DateTime>());Console.WriteLine("Zero = {0}", Zero.Value<string>()); 或者如果您更喜欢略有替代的Zero类:Or if you prefer a slightly alternative Zero class: public abstract class Zero<T>{ public static T Value { get { return default(T); } }}...Console.WriteLine("Zero = {0}", Zero<int>.Value);Console.WriteLine("Zero = {0}", Zero<DateTime>.Value);Console.WriteLine("Zero = {0}", Zero<string>.Value); 玩得开心! 干杯 Andi PS:C#不允许为类型定义扩展方法(例如 Int32.Zero )。您只能在类型的 对象上定义扩展方法(例如 42.Zero())。也许这就是你的困惑。Have fun!CheersAndiPS: C# does not allow to define extenstion methods for a type (e.g. Int32.Zero). You only can define extension methods on objects of a type (e.g. 42.Zero()). Maybe this is your confusion. public static class Generic { public const byte Zero = 0; public const sbyte SignedZero = 0; // will be needed on in one case} //Generic 显然,与所有其他数字类型一样,排除 sbyte 比字节更宽,它可以工作: Apparently, as all other numeric types, excluding sbyte are wider then byte, it works:uint ui = Generic.Zero;int i = Generic.Zero;long l = Generic.Zero;ulong ul = Generic.Zero;short sh = Generic.Zero;ushort ush = Generic.Zero;decimal d = Generic.Zero;double dbl = Generic.Zero;Single sng = Generic.Zero;byte b = Generic.Zero;// the only special case:sbyte sb = Generic.SignedZero; 当然只有字节和 sbyte 在没有显式转换的情况下不兼容,但所有其他数字类型都兼容 byte 和 sbyte 。 如果有这样的非对称模式(对 sbyte 或 byte :-))看起来有点难看,另一个选择是在有符号和无符号类型之间平均分配通用常量: Naturally only byte and sbyte are incompatible without explicit conversion, but all other numeric type are compatible with both byte and sbyte.If such asymmetric schema (not fair to either sbyte or byte :-)) seems a bit ugly, another option is to distribute the universal constants equally between signed and unsigned types:public static class Signed { public const sbyte Zero = 0;} // Signedpublic static class Unsigned { public const byte Zero = 0;} // Unsigned 在这种情况下,如果所有签名类型(包括两个浮点数)都使用 Signed.Zero ,并且所有无符号类型都使用无符号.Zero ,它会起作用。再次,它看起来有点多余,因为所有类型weider然后 byte 和 sbyte 可以使用任何一种变体。 好还是坏?问题是与隐式转换相关的一些性能成本。这是一个运行时操作,因此性能不如使用相同类型的常量或只有立即常量零,这在编译时是已知的。 (在某些语言中,例如Borland Pascal或Deplhi Pascal,有真正的泛型常量。未声明类型,编译器根据目标对象的类型生成实际的立即常量.C和C ++具有#define,这是一个明显的跛脚。) 什么更好?仍然,默认(TYPE)。所有的发明都应该用一些幽默来考虑,即使我们触及编程的严肃方面。 我差点忘了:定义 One 也很好,以确保我们可以完全摆脱任何代码中的所有立即常量。所有其他值都可以显式声明为常量,并放入一些带有定义的特殊文件,甚至放在数据文件和资源中。 另外,好好提一个类似的问题:null应该被认为是可接受的(顺便说一句,上面显示的方案也将涵盖可以为空的数字类型(如 int?)。至于,它应该首先被消灭唯一可接受的选择是 string.Empty 。 注意解决方案3,因为它显示在其中文本,一次涵盖所有类型,但 string.Empty 仍然需要默认(字符串)当然是null 。 -SA In this case, if all signed types (including both floating-point) use Signed.Zero, and all unsigned types use Unsigned.Zero, it will work. Again, it looks somewhat redundant, because all types weider then byte and sbyte can use either variant.Is it good or bad? The problem is some performance cost related to implicit conversion. This is a runtime operation, so the performance will be not as good as with same-type constant or just with immediate constant zero, which are known at compile time. (In some languages, such as Borland Pascal or Deplhi Pascal, there are "real" generic constants. Types are not declared, and the compiler generates real immediate constant depending on the type of target object. C and C++ have "#define", which is an apparent lame.)What''s better? Still, default(TYPE). All out inventions should be considered with some humor, even though we touch serious aspects of programming.I almost forgot: it''s also good to have the definition for One, to make sure that we can completely get rid of all immediate constants in any code. All other values can be made explicitly declared constants and put in some special files with definitions, or even in data files and resource.Also, good to mention one similar issue: null should be considered acceptable (by the way, the scheme shown above will also cover nullable numeric types (like int?). As to "", it should be exterminated first. The only acceptable alternative is string.Empty.Note that Solution 3, as it is shown in its text, covers all types at once, but string.Empty is still needed as default(string) is of course null.—SA 这篇关于Int32.Zero的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 02:45