I found this code in a project:
StringBuilder sb = new StringBuilder();
sb.Append("SELECT * FROM SomeTable")
.Append("WHERE SomeField = SomeValue")
.Append("ORDER BY etc etc etc")
at first I thought it was using a using statement but it's not. It works
because sb.Append returns an instance of itself. I'd be interested to hear
what everyone thinks of it.
--
Michael Culley 12 1452
Michael Culley <mc*****@NOSPAMoptushome.com.au> wrote: I found this code in a project:
StringBuilder sb = new StringBuilder(); sb.Append("SELECT * FROM SomeTable") .Append("WHERE SomeField = SomeValue") .Append("ORDER BY etc etc etc")
at first I thought it was using a using statement but it's not. It works because sb.Append returns an instance of itself. I'd be interested to hear what everyone thinks of it.
Assuming the next line uses "ToString", the developer would have been
better writing:
string x = "SELECT * FROM SomeTable "+
"WHERE SomeField=SomeValue "+
"ORDER BY etc etc etc";
That would actually be much faster than using StringBuilder, as:
1) If the value is actually a constant, it will be very fast indeed.
Concatenated string constants are concatenated by the C# compiler,
not at runtime.
2) If it's not, it ends up as one call to String.Concat, which is able
to allocate the right amount of space to start with - the
StringBuilder version will involve at least one reallocation.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hi,
It's ok , the Append method return the same instance so you are not
creating a new object
Now I assume that you replace the parameter in the Append() method, if not
it has an overhead, as you don't need to use StringBuilder for that. it
would be simpler to just do:
string s= "SELECT * FROM SomeTable WHERE SomeField = SomeValue .....";
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl... I found this code in a project:
StringBuilder sb = new StringBuilder(); sb.Append("SELECT * FROM SomeTable") .Append("WHERE SomeField = SomeValue") .Append("ORDER BY etc etc etc")
at first I thought it was using a using statement but it's not. It works because sb.Append returns an instance of itself. I'd be interested to hear what everyone thinks of it.
-- Michael Culley
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... [concatenation] would actually be much faster than using StringBuilder, as:
2) ... it ends up as one call to String.Concat, which is able to allocate the right amount of space to start with - the StringBuilder version will involve at least one reallocation.
Although, if the StringBuilder had been constructed with a buffer larger
than the default initial size of 16 bytes in the first place, it would also
avoid allocation. I mention this because passing an initial size and/or
string to the StringBuilder ctor is perhaps one of the most often-overlooked
optimizations, at least in the code I've seen. Quite often you know about
how big the final result will be, or you at least have an idea of the
average, and it's almost always way more than 16 bytes. By default,
StringBuilder will start with 16, then jump to 32, 64, 128, etc. In the
OP's contrived example, a starting size of perhaps 512, depending on the
expected length of the SQL statement, would probably be better and would
usually keep any new allocations to zero or one rather than perhaps a half
dozen.
--Bob
Bob Grommes <bo*@bobgrommes.com> wrote: [concatenation] would actually be much faster than using StringBuilder, as:
2) ... it ends up as one call to String.Concat, which is able to allocate the right amount of space to start with - the StringBuilder version will involve at least one reallocation.
Although, if the StringBuilder had been constructed with a buffer larger than the default initial size of 16 bytes in the first place, it would also avoid allocation. I mention this because passing an initial size and/or string to the StringBuilder ctor is perhaps one of the most often-overlooked optimizations, at least in the code I've seen. Quite often you know about how big the final result will be, or you at least have an idea of the average, and it's almost always way more than 16 bytes. By default, StringBuilder will start with 16, then jump to 32, 64, 128, etc. In the OP's contrived example, a starting size of perhaps 512, depending on the expected length of the SQL statement, would probably be better and would usually keep any new allocations to zero or one rather than perhaps a half dozen.
Absolutely. Still less efficient than doing it in a single set of
string concatenations (with no temporary strings involved), but better
than it currently is :)
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:ee*************@tk2msftngp13.phx.gbl... string s= "SELECT * FROM SomeTable WHERE SomeField = SomeValue .....";
But the actual sql was much too long for one line.
--
Michael Culley
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Absolutely. Still less efficient than doing it in a single set of string concatenations (with no temporary strings involved), but better than it currently is :)
I don't think the efficiency is the issue in the case, generally the string
is built once, a call is made across the network and a loop is executed
retrieving records. If it was updated from the least efficient to the most
efficient method of building the string I doubt any user would notice the
speed difference even if they were looking for it. Readability is what is
important. I probably do it in the least efficient method by using sql+= on
each line to build the string.
--
Michael Culley
Michael Culley <mc*****@NOSPAMoptushome.com.au> wrote: "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om... Absolutely. Still less efficient than doing it in a single set of string concatenations (with no temporary strings involved), but better than it currently is :)
I don't think the efficiency is the issue in the case, generally the string is built once, a call is made across the network and a loop is executed retrieving records. If it was updated from the least efficient to the most efficient method of building the string I doubt any user would notice the speed difference even if they were looking for it. Readability is what is important. I probably do it in the least efficient method by using sql+= on each line to build the string.
Why not just use a verbatim string literal then?
sql = @"
SELECT * FROM FOO
WHERE BAR
";
etc? Efficient, readable, very easy to edit... Wins all round as far as
I can see :)
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks Jon, I didn't know you could do that. However, I'm not a big fan
because it kills your indenting, AFAIK you need to write it like this:
[STAThread]
static void Main()
{
string sql = @"SELECT *
FROM SomeTable
WHERE ID = 1";
MessageBox.Show(sql);
}
--
Michael Culley
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Michael Culley <mc*****@NOSPAMoptushome.com.au> wrote: "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om... Absolutely. Still less efficient than doing it in a single set of string concatenations (with no temporary strings involved), but better than it currently is :)
I don't think the efficiency is the issue in the case, generally the
string is built once, a call is made across the network and a loop is executed retrieving records. If it was updated from the least efficient to the
most efficient method of building the string I doubt any user would notice
the speed difference even if they were looking for it. Readability is what
is important. I probably do it in the least efficient method by using sql+=
on each line to build the string.
Why not just use a verbatim string literal then?
sql = @" SELECT * FROM FOO WHERE BAR ";
etc? Efficient, readable, very easy to edit... Wins all round as far as I can see :)
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Michael Culley <mc*****@NOSPAMoptushome.com.au> wrote: Thanks Jon, I didn't know you could do that. However, I'm not a big fan because it kills your indenting, AFAIK you need to write it like this:
[STAThread] static void Main() { string sql = @"SELECT * FROM SomeTable WHERE ID = 1"; MessageBox.Show(sql); }
No, you can put the indenting in yourself. Of course, the whitespace
will still be in the SQL, but that shouldn't make any real difference,
should it? I'd rather have it indented nicely in the code than in the
server :)
Here's another alternative:
string sql = "SELECT * "+
"FROM SomeTable "+
"WHERE ID = 1";
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hi,
What you mean with that?
If you mean that you dont like to have a very long line in the editor you
have not to worry about it
These two lines should compile to the same:
string s = "1" +
"2" +
"3" +
"4";
string s = "1234";
The compiler should be smart enough to create one string only, I haven't
test it though.
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:uG*************@tk2msftngp13.phx.gbl... "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:ee*************@tk2msftngp13.phx.gbl... string s= "SELECT * FROM SomeTable WHERE SomeField = SomeValue .....";
But the actual sql was much too long for one line.
-- Michael Culley
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote: What you mean with that?
If you mean that you dont like to have a very long line in the editor you have not to worry about it
These two lines should compile to the same:
string s = "1" + "2" + "3" + "4"; string s = "1234";
The compiler should be smart enough to create one string only, I haven't test it though.
Indeed, it will - and it's required to by the spec.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... No, you can put the indenting in yourself. Of course, the whitespace will still be in the SQL, but that shouldn't make any real difference, should it? I'd rather have it indented nicely in the code than in the server :)
It can be a pain for debugging tho.
string sql = "SELECT * "+ "FROM SomeTable "+ "WHERE ID = 1";
This is probably the best solution (in my opinion of course :-)
--
Michael Culley This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tony Marston |
last post by:
Several months ago I started a thread with the title "What is/is not
considered to be good OO programming" which started a long and
interesting discussion.
I have condensed the arguments into a...
|
by: darwinist |
last post by:
What PHP Represents
There is no shortage of complaints one could make about php as a
language, and although the list does shrink with each release, some of
them are inherent to the origins and...
|
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: Reed L. O'Brien |
last post by:
I see rotor was removed for 2.4 and the docs say use an AES module
provided separately... Is there a standard module that works alike or
an AES module that works alike but with better encryption?...
|
by: Philippe C. Martin |
last post by:
I apologize in advance for launching this post but I might get enlightment
somehow (PS: I am _very_ agnostic ;-).
- 1) I do not consider my intelligence/education above average
- 2) I am very...
|
by: Sarah Tanembaum |
last post by:
Beside its an opensource and supported by community, what's the fundamental
differences between PostgreSQL and those high-price commercial database (and
some are bloated such as Oracle) from...
|
by: typingcat |
last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so
on. I've tried many PHP IDEs today, but almost non of them supported
Unicode (UTF-8) file.
I've found that the only Unicode...
|
by: Keith K |
last post by:
Having developed with VB since 1992, I am now VERY
interested in C#. I've written several applications with
C# and I do enjoy the language.
What C# Needs:
There are a few things that I do...
|
by: Jason Huang |
last post by:
Hi,
Would someone explain the following coding more detail for me? What's the
( ) for?
CurrentText = (TextBox)e.Item.Cells.Controls;
Thanks.
Jason
|
by: Pat |
last post by:
Hi,
I've run into a strange problem, but one that seems like it might be
fairly common.
I have a single base class, from which several other classes are derived.
To keep the example simple,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |