Skip to content

Replacing the ToString overloads with extensions #1552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,27 +1162,6 @@ public override string ToString()
return ToString(null, null);
}}

/// <summary>
/// Gets the default string representation of value and unit using the given format provider.
/// </summary>
/// <returns>String representation.</returns>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public string ToString(IFormatProvider? provider)
{{
return ToString(null, provider);
}}

/// <inheritdoc cref=""QuantityFormatter.Format{{TQuantity}}(TQuantity, string?, IFormatProvider?)""/>
/// <summary>
/// Gets the string representation of this instance in the specified format string using <see cref=""CultureInfo.CurrentCulture"" />.
/// </summary>
/// <param name=""format"">The format string.</param>
/// <returns>The string representation.</returns>
public string ToString(string? format)
{{
return ToString(format, null);
}}

/// <inheritdoc cref=""QuantityFormatter.Format{{TQuantity}}(TQuantity, string?, IFormatProvider?)""/>
/// <summary>
/// Gets the string representation of this instance in the specified format string using the specified format provider, or <see cref=""CultureInfo.CurrentCulture"" /> if null.
Expand Down
1 change: 0 additions & 1 deletion UnitsNet.Tests/CustomQuantities/HowMuch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public IQuantity ToUnit(Enum unit)

public override string ToString() => $"{Value} {Unit}";
public string ToString(string? format, IFormatProvider? formatProvider) => $"HowMuch ({format}, {formatProvider})";
public string ToString(IFormatProvider? provider) => $"HowMuch ({provider})";

#endregion
}
Expand Down
51 changes: 51 additions & 0 deletions UnitsNet/Extensions/QuantityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.

using System;
using System.Globalization;

namespace UnitsNet;

/// <summary>
/// Provides extension methods applicable to all quantities in the UnitsNet library.
/// </summary>
public static class QuantityExtensions
{
/// <summary>
/// Returns the string representation of the specified quantity using the provided format provider.
/// </summary>
/// <typeparam name="TQuantity">
/// The type of the quantity, which must implement <see cref="IQuantity" /> and
/// <see cref="IFormattable" />.
/// </typeparam>
/// <param name="quantity">The quantity to convert to a string.</param>
/// <param name="formatProvider">
/// The format provider to use for localization and number formatting.
/// If <c>null</c>, the default is <see cref="CultureInfo.CurrentCulture" />.
/// </param>
/// <returns>A string representation of the quantity.</returns>
public static string ToString<TQuantity>(this TQuantity quantity, IFormatProvider? formatProvider)
where TQuantity : IQuantity, IFormattable
{
return quantity.ToString(null, formatProvider);
}

/// <summary>
/// Returns the string representation of the specified quantity using the provided format string.
/// </summary>
/// <typeparam name="TQuantity">
/// The type of the quantity, which must implement <see cref="IQuantity" /> and
/// <see cref="IFormattable" />.
/// </typeparam>
/// <param name="quantity">The quantity to convert to a string.</param>
/// <param name="format">
/// The format string to use for formatting the quantity.
/// If <c>null</c> or empty, the default format is used.
/// </param>
/// <returns>A string representation of the quantity.</returns>
public static string ToString<TQuantity>(this TQuantity quantity, string? format)
where TQuantity : IQuantity, IFormattable
{
return quantity.ToString(format, null);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions UnitsNet/GeneratedCode/Quantities/Angle.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions UnitsNet/GeneratedCode/Quantities/Area.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions UnitsNet/GeneratedCode/Quantities/AreaDensity.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions UnitsNet/GeneratedCode/Quantities/AreaMomentOfInertia.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions UnitsNet/GeneratedCode/Quantities/BitRate.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading