String Concatenation unsafe in C#, need to use Str

2019-01-20 10:50发布

My question is this: Is string concatenation in C# safe? If string concatenation leads to unexpected errors, and replacing that string concatenation by using StringBuilder causes those errors to disappear, what might that indicate?

Background: I am developing a small command line C# application. It takes command line arguments, performs a slightly complicated SQL query, and outputs about 1300 rows of data into a formatted XML file.

My initial program would always run fine in debug mode. However, in release mode it would get to about the 750th SQL result, and then die with an error. The error was that a certain column of data could not be read, even through the Read() method of the SqlDataReader object had just returned true.

This problem was fixed by using StringBuilder for all operations in the code, where previously there had been "string1 + string2". I'm not talking about string concatenation inside the SQL query loop, where StringBuilder was already in use. I'm talking about simple concatenations between two or three short string variables earlier in the code.

I had the impression that C# was smart enough to handle the memory management for adding a few strings together. Am I wrong? Or does this indicate some other sort of code problem?

8条回答
Fickle 薄情
2楼-- · 2019-01-20 11:00

One cause may be that strings are immutable in .Net so when you do an operation on one such as concatenation you are actually creating a new string.

Another possible cause is that string length is an int so the maximum possible length is Int32.MaxValue or 2,147,483,647.

In either case a StringBuilder is better than "string1 + string2" for this type of operation. Although, using the built-in XML capabilities would be even better.

查看更多
爷的心禁止访问
3楼-- · 2019-01-20 11:01

When compounding strings together I always use StringBuilder. It's designed for it and is more efficient that simply using "string1 + string2".

查看更多
做个烂人
4楼-- · 2019-01-20 11:06

How long would it take the concatenation version vs the string builder version? It's possible that your connection to the DB is being closed. If you are doing a lot of concatenation, i would go w/ StringBuilder as it is a bit more efficient.

查看更多
放我归山
5楼-- · 2019-01-20 11:09

Apart from what you're doing is probably best done with XML APIs instead of strings or StringBuilder I doubt that the error you see is due to string concatenation. Maybe switching to StringBuilder just masked the error or went over it gracefully, but I doubt using strings really was the cause.

查看更多
不美不萌又怎样
6楼-- · 2019-01-20 11:09

String concatenation is safe though more memory intensive than using a StringBuilder if contatenating large numbers of strings in a loop. And in extreme cases you could be running out of memory.

It's almost certainly a bug in your code.

Maybe you're contatenating a very large number of strings. Or maybe it's something else completely different.

I'd go back to debugging without any preconceptions of the root cause - if you're still having problems try to reduce it to the minimum needed to repro the problem and post code.

查看更多
可以哭但决不认输i
7楼-- · 2019-01-20 11:09

string.Concat(string[]) is by far the fastest way to concatenate strings. It litterly kills StringBuilder in performance when used in loops, especially if you create the StringBuilder in each iteration. There are loads of references if you Google "c# string format vs stringbuilder" or something like that. http://www.codeproject.com/KB/cs/StringBuilder_vs_String.aspx gives you an ideer about the times. Here string.Join wins the concatenation test but I belive this is because the string.Concat(string, string) is used instead of the overloaded version that takes an array. If you take a look at the MSIL code that is generated by the different methods you'll see what going on beneath the hood.

查看更多
登录 后发表回答