Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Well, Mat's MugMat's Mug already gave the only improvement for the code in the extension-methodthe only improvement for the code in the extension-method I could find, using the conditional-member-access operator.

Aside from that, there's only the methods name. Which is far too long and awkward.
Truncate has a nice ring to it, and everybody understands it.

public static class StringExtensions
{
    public static string Truncate(this string value, int maxLength)
    {
        return value?.Substring(0, Math.Min(value.Length, maxLength));
    }
}

Well, Mat's Mug already gave the only improvement for the code in the extension-method I could find, using the conditional-member-access operator.

Aside from that, there's only the methods name. Which is far too long and awkward.
Truncate has a nice ring to it, and everybody understands it.

public static class StringExtensions
{
    public static string Truncate(this string value, int maxLength)
    {
        return value?.Substring(0, Math.Min(value.Length, maxLength));
    }
}

Well, Mat's Mug already gave the only improvement for the code in the extension-method I could find, using the conditional-member-access operator.

Aside from that, there's only the methods name. Which is far too long and awkward.
Truncate has a nice ring to it, and everybody understands it.

public static class StringExtensions
{
    public static string Truncate(this string value, int maxLength)
    {
        return value?.Substring(0, Math.Min(value.Length, maxLength));
    }
}
Source Link
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65

Well, Mat's Mug already gave the only improvement for the code in the extension-method I could find, using the conditional-member-access operator.

Aside from that, there's only the methods name. Which is far too long and awkward.
Truncate has a nice ring to it, and everybody understands it.

public static class StringExtensions
{
    public static string Truncate(this string value, int maxLength)
    {
        return value?.Substring(0, Math.Min(value.Length, maxLength));
    }
}