Re: C# Multi-line String Literals

Hi: OK, first a StringBuilder creates one variable to hold the string. No 
matter how many times you change that string you will have one variable and 
that will save space and perform faster than using a standard string variable 
if you change the value of the StringBuilder object more than a few hundred 
times or so. A standard String variable is created new each time you change the 
contents of the string so you can get a bunch of variables created if you 
change the value held in the string. Now, it looks like you are trying to build 
a Database Query String in a StringBuilder or String Variable. In your example 
you include the where clause and the value to test for "5" if I remember in the 
string itself. This is not how you would creat4e a Query String if you are 
going to have diferent values for a where clause or other clauses that might 
have values that would change. You can build a query string and use it by 
passing the string and a value to the DB Engine but the correct method would be 
to use a Parameterized query. In your example the query might execute ok but 
what if you wanted to use a text value in the where clause? Then you would have 
to use single quotes or, depending on the DB Engine settings use Single quotes 
for the query string and double quotes for the value. You want to use a 
Parameterized query if you are learning to work with a Sql Server or Sql CE 
Database, likely an Access DB or other DB if Parameterized querys are supported 
for non MS Databases as well. 
So, you can use either StringBuilder or a standard string to build a string. By 
the way, I am not sure about Multi-Line strings, a string is one long line of 
text as far as I know. You can include line breaks in a string but the string 
itself is one long line of text. If you want multi-lines you would use a list 
object or a table or array object to hold each line of text. A list might look 
like:
List.( Of String). Then you can use the list of strings by using the methods 
and properties of the List Object like the Append, For Each, Remove and all 
that jazz. But, it sounds like you just want to build either a standard query 
string or want to use Parameterized queries for the code you are working on. 
PS, my list object code is from vb.net but I believe it is a Framework object 
so should be the same in C# or other VS Languages. Well, sorry this is sort of 
long but a simple answer is that there is actually no such thing as a 
multi-line string. You can code several lines to build a string and variious 
objects that hold strings like an array or list but each string is one long 
group of characters so I don't understand the question exactly.
In the MS Docs the StringBuilder AppendLine just addes a line break in the long 
string you are building:
According to the MS Docs:
StringBuilder.AppendLine Method
Appends the default line terminator to the end of the current
StringBuilder
Well, my answer is to use the simple string object unless you will be changing 
the string variable more than say 50 or 100 times without destroying it by 
leaving the class or method where it is defined so you don't build thousands of 
variables, one each time you change the value of the string.
Rick USA
 ----- Original Message ----- 
  From: Kerneels Roos 
  To: programmingblind 
  Sent: Wednesday, August 11, 2010 3:57 AM
  Subject: C# Multi-line String Literals


  Hi List,

  Three ways to do this, which one is best:

  1.
  StringBuilder sb = new StringBuilder();
  sb.AppendLine("SELECT id ");
  sb.AppendLine("FROM my_table ");
  sb.AppendLine("WHERE id > 5");

  2.
  string myMultiLine = "SELECT id "
     + "FROM my_table"
     + "WHERE id >5";

  3.
  string myMultiLine = @"SELECT id
     FROM my_table
     WHERE id > 5";

  Method 1 is the usual way, 2 introduces extra whitespace and lime breaks, and 
3 apparently does not result in slower code if the whole string is known at 
compile time -- the compiler is smart enough to make it into one string (see: 
http://jameskovacs.com/2007/02/12/multiline-strings-in-c/ ).

  If you know that you are going to have to tweak the string and have a few 
round trips to the query analyzer I would say that 2 would actually be best 
since you only have to remove the first @" and last "; and the literal string 
will run. Once you know the string is perfect I would say method 3 is the best 
since it looks like it might be faster than method 1 and does not have the 
extra, maybe unwanted additional whitespace and new line chars that method 2 
introduces.

  What do you guys think?

  Regards,

  -- 
  Kerneels Roos
  Cell/SMS: +27 (0)82 309 1998
  Skype: cornelis.roos

  The early bird may get the worm, but the second mouse gets the cheese!


Other related posts: