Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, October 23, 2015 1:50 PM
I have a block of code like this:
message="This is the message";
var body = Encoding.UTF8.GetBytes(message)
and that works pretty good. This is for a RabbitMQ project and I want to increase the size of message substantially so I thought I'd use the StringBuilder class like this:
StringBuilder message = new StringBuilder();
message.Append("number 1");
message.Append("number 2");
message.Append("number 3");
message.Append("number 4");
message.Append("number 5");
message.Append("number 6");var body = Encoding.UTF8.GetBytes(message);
But this gives me the error, "Cannot convert from 'System.Text.StringBuilder' to 'char[]'
All replies (3)
Friday, October 23, 2015 1:54 PM ✅Answered
Okay, and just as quickly by trial and error I found this at least gets rid of the error:
var body = Encoding.UTF8.GetBytes(message.ToString());
But since the guys in IT are still setting up the RabbitMQ server is this really as simple a fix as it appears or will I possibly have trouble with it?
Friday, October 23, 2015 4:03 PM ✅Answered
Questions related to RabbitMQ should be posted in their forums.
In regards to your compiler error then calling ToString gives you back the string form of the builder content. It is semantically equivalent to your first block of code. If the first block worked then there is no reason why this wouldn't as well.
Michael Taylor
http://blogs.msmvps.com/p3net
Friday, October 23, 2015 5:48 PM
SringBuilders primary purpose is to work around several special things in the .NET Framework and how it handles strings:
Strings are a class. Yet Strings are inmutable (one of the few classes that is). You can't change a string, only create a new string instance with different value.
Strings compare using value semantics
Strings are also interned. If two strings references would have the same value, they actually get the same instance of string assigned (at least for the 2nd and any later one).
If you do a lot of string connaction in a row (like in a loop), the net result would be that each operation would:
- Create a new, slightly longer string instance. The old one would still have to be garbage collected, bloatng memory usage and increasing GC runtime.
- The runtime/GC actually looks in the intern pool if it got that one already at every assignment.
That is bad for both memory use and performance. StringBuilders primary purpose is to allow you to make a lot of string connaction without either of those two things being triggered along the line.
Also note that:
string message = "number 1" + "number 2" + "number 3" + "number 4" + "number 5" + "number 6";
will be optimised to be one string connaction, not 5 in sequence. You only need to use string builder if you got a lot of connactions going on, like in a loop. Otherwise just writing them into one line of code should be enough.