Multiline string literals in c#

Having programmed in Python for the last year and now moving over to csharp, one of the things that irritated me about csharp was how it handled strings.

For example, in python, assigning a comma seperated object representation to a string variable would simply be:

var = “%s,%s,%s”%(obj1,ob2,ob3)

whereas in c#, it would be:

var = obj1.ToString()+”,”+obj2.ToString()+”,”+obj3.ToString()

This still pisses me off and I still haven’t found a more elegant way to do it and it’s only basic stuff.

The other is multiline strings.

eg, a multiline in python would simply be

var = “”"this is

a

multiline string”"”

in c#, until now, I’ve been using the longwinded way of:

var = “this is \n”

+”a \n”

+”multiline string”"
Until I’ve now discovered you can do it like:

var = @”this is
a
multiline string”

Wow, you learn something new everyday, how could I have not known this before, something so basic. I still prefer to code python and some things in c# and the .NET Framework really bug me but things are getting better

3 Responses

  1. Philroche Says:

    Hi Sam,

    You can use String.Format() for string substitution just like you do in Python. I’ve found it very useful recently.

    Phil

  2. sam sinfield Says:

    HI Phil,

    Joel mentioned about using String.Format() but didn’t post the comment so you got in first! I’ve had a quick look at it. It’s a big improvement over the long way, still prefer python though

  3. Matt Wilcox Says:

    Is the @ suppressing error messages as it does in PHP, or does it actually alter the way a string is interpreted?

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.