string length

Hi, I have a string which has the length 180857866. Is that okay for c# or 4.0)
Who is Participating?
The Length property returns an int, so the maximum length of a string is Int32.MaxValue (2,147,483,647) characters. Practically this will probably mean that you can make your string as long as you have memory available to put it in.

Your more likely to be hitting memory problems than string length problems. Instead of doing += use a stringbuilder.
Even if it's a 64-bit machine, you cannot create an object that is greater than 2GB in size within .NET.  I believe each character in a string is allocated 2 bytes; therefore, your string would consume about 344MB of space.

So, no, your string shouldn't be a problem.

Of course, there's always the question of why you're manipulating such large strings?
System.String is a class in .net framework. It doesnt matter whether its vb or c#.both are complied into
Éric MoreauSenior .Net ConsultantCommented:
>>Instead of doing += use a stringbuilder.

Notice that the StringBuilder has the same Int32.MaxValue limitation.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.