Skip to content

Remove conversion overloads from ToString implementations #545

@tmilnthorp

Description

@tmilnthorp

I would like to split up the code responsible for unit conversion and ToString.

Since we have the ToUnit method, we could simplify the ToString implementations and implement IFormattable.

This would leave the following ToString overloads:

public override string ToString();
public string ToString(string format);
public string ToString(IFormatProvider provider);
public string ToString(string format, IFormatProvider provider);

For example to get a length string in inches, we currently do the following:

var length = Length.FromMeters( 3.0 );
var lengthInInchesString = length.ToString( LengthUnit.Inch );

Today, this will achieve the same thing

var length = Length.FromMeters( 3.0 );
var lengthInInchesString = length.ToUnit( LengthUnit.Inch ).ToString();

We could even change the conversion properties to return a converted quantity rather than a double (public Length Inches in this example) and do the following:

var length = Length.FromMeters( 3.0 );
var lengthInInchesString = length.Inches.ToString();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions