C# Value Type Class Template
I'm writing/written a value type struct and was wondering if there was a guide that contains a standard template for method signatures and interface implementation. I thought I'd start by looking at the System.Double documentation:
struct def
public struct Double : IComparable, IFormattable,
IConvertible, IComparable<double>, IEquatable<double>
Double Methods
Is there like a standard approach to creating a value type struct?
T开发者_运维百科hanks
There isn't a template per se, but there are certainly guidelines. The most important guideline is value types should be immutable. Only violate this guideline if you know exactly what you are doing.
In addition to the guidelines listed in the MSDN article I linked to I also recommend:
- Override
Object.Equals
andObject.GetHashCode
. - Implement
==
and!=
.
精彩评论