Re: C# Multi-line String Literals

  • From: Kerneels Roos <kerneels@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Thu, 12 Aug 2010 08:42:04 +0200

Thanks Jacques, I also think option 3 is quite neat. You have to get to
hundreds, maybe even thousands of regular lenght lines before the compiler
will have no option of splitting it into two literals. I think ORM systems
are great for most apps -- this thread is however not about the merits of
ORM systems, but I'll say quickly that having worked with NHibernate for a
while before, and realising the complexity that some SQL queries require,
and the performance gains of using cleverly designed queries, I think there
will always be a need for raw SQL in code or at least read from a file or
invoked by a stored procedure.

I don't think there exists an ORM system that can easily do what you can do
with multiple joins, sub queries and so on. It might be possible but either
inefficient with an ORM or ineficient to code with an ORM -- very dificult.

Now, to wrap this up I'll just state again, to my knowledge, the compiler
creates one string in the byte code from a statement like this:

string myLongString = @"line 1,
    line 2
   line 3
   line 4
   ...
   line 100";

If the code appears on a very indented level  you will get extra whitespace
in your string, but it's not the end of the world .

Keep well


On Thu, Aug 12, 2010 at 6:18 AM, black ares
<matematicianu2003@xxxxxxxxxxx>wrote:

>  or use sql strings out of code if you want to use your own ORM classes.
>
>
> ----- Original Message -----
> *From:* Jacques Bosch <jfbosch@xxxxxxxxx>
> *To:* programmingblind@xxxxxxxxxxxxx
> *Sent:* Wednesday, August 11, 2010 11:21 PM
> *Subject:* Re: C# Multi-line String Literals
>
> 3rd option definitely best for readability.
> In terms of performance, for fixed number of concats, or anything less than
> multiple thousands for that matter, you will notice very negligible
> difference.
> But what are you coding SQL in your strings for anyway. <G> Use and ORM.
>
>
> ----- Original Message -----
> *From:* Kerneels Roos <kerneels@xxxxxxxxx>
> *To:* programmingblind <programmingblind@xxxxxxxxxxxxx>
> *Sent:* Wednesday, August 11, 2010 9: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: