Search code examples
.nettypesreferencesystem.version

Why is the 'System.Version' a reference type (class) and not a value type (struct)?


If I look at the members of the .NET type System.Version it might be a better idea to create such a type as value type (C# struct) instead of a reference type (class).

What might be the reasons or arguments that Microsoft decided to implement that type in that way? Is it simply a kind of "bug" / mistake / inconsequence or a "feature" (in other words: "really wanted and useful"?

There are methods like Parse() and TryParse() which are typical for value types, e.g. System.Int32, System.Double or System.Guid. The properties of System.Version only store short and integer values (s. System.TimeSpan), the type itself is comparable (with operators) and as struct it might not be necessary to make it cloneable.

I do not have anything against that "status quo", it is only to satisfy my curiosity ;-)

EDIT: Added comparison to System.TimeSpan.


Solution

  • According to Microsoft's own guidelines, you should prefer a class to a structure when the instance is at least 16 bytes. Since System.Version stores four Int32, each consisting of four bytes for a total of 16 bytes, they followed their own advice and made it a class.