Shortcut Formatting: A feature request for C# vNext!

I’ve been pondering what possible features could be added to C# after version 3.5 of the compiler. For sure things like parallelism constructs are on the radar for the language team at Microsoft, but while they are at it I would like them to tackle a simpler challenge as well – a shortcut for formatting strings.

Let’s say that my code is going to throw and exception, I typically need to build the error message to load into the exception message. That could be expressed as:

throw new Exception(string.Format(“{0}{1}{2}”, a, b, c));

But I think we could have a nicer inline string format syntax:

throw new Exception(  @(“{0}{1}{2}”|a|b|c)  );

I’m not sure about the syntax, but the idea is a more compact syntax that would look good inline, especially if you had nested object constructions, like a LINQ projection.

11 thoughts on “Shortcut Formatting: A feature request for C# vNext!

  1. Alex

    um… dude, that’s horrible… just do ruby strings plz…

    throw new Exception(“{a}{b}{c}”);

    surely compiler can retwire it to String.Format()… if MS needs some help, I already have C# code to do just that🙂

  2. Jason Stangroome

    The String.Format in your example is a little verbose but it really gets out of hand when you start playing ball with Code Analysis rule CA1305:

    String.Format(CultureInfo.CurrentCulture, “{0}{1}{2}”, a, b, c);

    Not sure how I’d work the IFormatProvider into your shortcut notation.

  3. Pingback: ¿Qué quieres desarrollar hoy? : C# vNext wishlist: comma less argument list

  4. Pingback: String interpolation feature wish in C# 4.0 at Lanwin dev home

  5. Mitch Denny Post author

    All good feedback. I think I like the parameter substitution approach the best e.g.:

    “{a}{b}{c}”

    Having said that I think that the string would need a suffix so that the compiler knew to interpret it differently, for example:

    @@”{a}{b}{c}”

    Then, you could also use standard format string syntax as well to format the parameters:

    @@”{a:c}[b}{c}”

  6. Pingback: PaulStovell.NET » String.Format Extension Method

  7. Pingback: ¿Qué quieres desarrollar hoy? : Lo que quisiera ver en la siguiente version de C#

  8. Moritz Fechtner

    ///
    /// Uses the current String as Format and paste args to String.Format
    ///
    /// String format
    /// Object[] args
    /// System.String
    public static String Args(this String format, params object[] args)
    {
    return String.Format(format, args);
    }

    ///
    /// Uses the current String as Format and paste args to String.Format
    ///
    /// String format
    /// IFormatProvider formatProvider
    /// Object[] args
    /// System.String
    public static String Args(this String format, IFormatProvider formatProvider, params object[] args)
    {
    return String.Format(formatProvider, format, args);
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s