.NET Tips and Tricks

Blog archive

How to Build .NET Strings at Run Time

You want to put together an error message that includes information from the Customer object that caused the problem. Since you're not repeatedly modifying a single string variable, using StringBuilder would be overkill. You still have three options.

Your first option is the most obvious: Concatenation. Here's some code that assembles a message from constant string and some values:

string msg = "Customer " + cust.Id + " has an invalid status " + cust.Status;

Your second option is to use the string class's Format method. You're probably used to seeing this syntax because there are lots of functions that use string.Format under the hood to build strings. With the Format method, you provide your constant string as your first parameter. Inside that string you put place holders (marked with curly braces: { }) where the variable data is to be inserted. The Format method will insert the values from the following parameters into the place holders.

Here's my message with the Format method:

string msg = string.Format("Customer {0} has an invalid status {1}.", cust.Id, cust.Status);

Your third option is string interpolation. This is the newest mechanism and is what all the "hip and happening" developers use. It looks much like using the Format method, but the placeholders now hold the values to be inserted into the string. You need to flag the string with a dollar sign ($) to tell C# that the curly braces are to be treated as placeholders rather than included in the string:

string msg = $"Customer {cust.Id} has an invalid status {cust.Status}.";

Posted by Peter Vogel on 05/29/2019


comments powered by Disqus

Featured

  • Linear Regression Using JavaScript

    Dr. James McCaffrey presents a complete end-to-end demonstration of linear regression using JavaScript. Linear regression is the simplest machine learning technique to predict a single numeric value, and a good way to establish baseline results for comparison with other more sophisticated regression techniques.

  • Creating Simple Chat Bots with Microsoft Fabric Datastores

    At Visual Studio Live! San Diego, Ginger Grant of Desert Isle Group will lead a practical, demo-driven session on how to build simple yet powerful chatbots using Microsoft Fabric lakehouses and warehouses. Attendees will learn how to use AI skills and grounding techniques to enable conversational data access -- quickly and cost-effectively. Ideal for developers ready to extend analytics with conversational interfaces.

  • Prompt Engineering? See VS Code Team's System Prompts for Copilot Chat, Now Open Source

    "Explore the codebase and learn how agent mode is implemented, what context is sent to LLMs, and how we engineer our prompts."

  • VS Code Goes Transparent as Open-Source AI Editor

    Microsoft has open sourced the GitHub Copilot Chat extension, marking a major step in turning VS Code into an open-source AI editor focused on transparency, collaboration, and community-driven development.

  • New Default Model for Visual Studio Copilot, So How Do You Choose?

    Along with a new default model, a new Consumptions panel in the IDE helps developers monitor their usage of the various models, paired with UI to help easily switch among models.

Subscribe on YouTube