Re: C# Multi-line String Literals

  • From: "black ares" <matematicianu2003@xxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 11 Aug 2010 17:38:39 +0300

on the other way, from a technical leader point of view,
sql code has nothing to do directly in c# code if you not use special features 
like linq sau entity framework.
Even there you can avoid inserting sql code directly in the c# code.
Reasons?
If you embed sql in your code, when ever an query customisation is needed by 
the customer
you must rebuild and redeploy your entire application or, atleast, a part of it.
Also, when some one is debuging the c# code he will be confused to search 
pieces of c# amoung sql queries.
This concept is called aspect separation.
In the c# code, you must have only c#code.
More over, this wqay you will get a more dynamic application, because you can 
change the way it is working, with out touch the code.


  ----- Original Message ----- 
  From: Kerneels Roos 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Wednesday, August 11, 2010 12:03 PM
  Subject: Re: C# Multi-line String Literals


  Thanks for all the info Rick. I don't mind long emails if they are 
interesting and convey good info ha ha!

  Oh yeah, I totally agree with you about actual values which one will do via 
parameterised queries. What I meant with multi line strings is simply that, for 
a big query you don't want to put it all on one line -- it becomes very hard to 
read it like that. Splitting it across lines is a much better approach. 
Previously I thought that the "+" string concatenation operator was very slow, 
but now it looks like it's not really a problem if the string is a constant, 
meaning, the value is totally known at compile time.

  Some guy inspected the actual byte code and saw that the compiler is smart 
enough to make one long string if it's just a constant string value, even if 
you use "+" operators. The "@" operator for denoting very literal string is 
definately also treated like that by the compiler with the exception that you 
introduce a glut of white space and new line chars since the compiler can 
surely not remove those, that would attack the integrity of your code itself or 
data rather.

  StringBuilder is for sure by far the best way to go if you are going to 
append to a string in a loop or something -- append a variable number of times, 
but my question is just what about if you are appending a fixed number of 
times, like when you want to display some sql string literal more reader 
friendly, is the StringBuilder approach still more eficient? It cant' b.

  Regards


  On Wed, Aug 11, 2010 at 10:43 AM, RicksPlace <ofbgmail@xxxxxxxxx> wrote:

    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!






  -- 
  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: