By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,436 Members | 2,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,436 IT Pros & Developers. It's quick & easy.

Maximum length of a String?

P: n/a
Hello,

does a String in C# have a maximum length?

I tried to write a ToString Method of my class containing a hashtable.
At the beginning of the method i defined a String "ret".

In every iteration of my hashtable I add the content with ret += "...".

After some time my programm crashes.

If i use Console.Writeln in every iteration instead of ret += ...,
then everything works fine.

So I guess it has something to do with a possible maximum length of a
string?
Regards,

Martin
Sep 14 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi Martin,

The string itself should handle lengths well above your future memory
limits, but massive string concatenations won't work well if the hashtable
is large.

Instead of ret += "..." use StringBuilder.Append
On Thu, 14 Sep 2006 12:31:56 +0200, Martin Pöpping
<ma******@despammed.comwrote:
Hello,

does a String in C# have a maximum length?

I tried to write a ToString Method of my class containing a hashtable.
At the beginning of the method i defined a String "ret".

In every iteration of my hashtable I add the content with ret += "...".

After some time my programm crashes.

If i use Console.Writeln in every iteration instead of ret += ...,
then everything works fine.

So I guess it has something to do with a possible maximum length of a
string?
Regards,

Martin


--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 14 '06 #2

P: n/a
String length is an int, which has a max value of (2,147,483,647)
characters. Your more likely to be hitting memory problems than string
length problems. Instead of doing += use a stringbuilder.

http://msdn.microsoft.com/library/de...ilderclass.asp

--
Regards

John Timney (MVP)
Visit my website:
http://www.johntimney.com
"Martin Pöpping" <ma******@despammed.comwrote in message
news:ee**********@newsreader2.netcologne.de...
Hello,

does a String in C# have a maximum length?

I tried to write a ToString Method of my class containing a hashtable.
At the beginning of the method i defined a String "ret".

In every iteration of my hashtable I add the content with ret += "...".

After some time my programm crashes.

If i use Console.Writeln in every iteration instead of ret += ...,
then everything works fine.

So I guess it has something to do with a possible maximum length of a
string?
Regards,

Martin

Sep 14 '06 #3

P: n/a
John Timney (MVP) schrieb:
String length is an int, which has a max value of (2,147,483,647)
characters. Your more likely to be hitting memory problems than string
length problems. Instead of doing += use a stringbuilder.

http://msdn.microsoft.com/library/de...ilderclass.asp
Thanks to all!

It works fine now.
Regards,

Martin
Sep 14 '06 #4

P: n/a
Martin Pöpping wrote:

<snip>
Thanks to all!

It works fine now.
For more info on where/why to use StringBuilder, see
http://www.pobox.com/~skeet/csharp/stringbuilder.html

Jon

Sep 14 '06 #5

P: n/a
John ...

If I remember correctly A few of the upper bits of the length (I believe 3
but don't quote me) are used as flags for other information .. so I believe
this limits it in the MS implementation to 28 bits in length which is
substantially shorter ... of course you are quite unlikely to hit this as
the limit will tend to be the largest contiguous area of memory available.

Cheers,

Greg

"John Timney (MVP)" <x_****@timney.eclipse.co.ukwrote in message
news:0_********************@eclipse.net.uk...
String length is an int, which has a max value of (2,147,483,647)
characters. Your more likely to be hitting memory problems than string
length problems. Instead of doing += use a stringbuilder.

http://msdn.microsoft.com/library/de...ilderclass.asp

--
Regards

John Timney (MVP)
Visit my website:
http://www.johntimney.com
"Martin Pöpping" <ma******@despammed.comwrote in message
news:ee**********@newsreader2.netcologne.de...
>Hello,

does a String in C# have a maximum length?

I tried to write a ToString Method of my class containing a hashtable.
At the beginning of the method i defined a String "ret".

In every iteration of my hashtable I add the content with ret += "...".

After some time my programm crashes.

If i use Console.Writeln in every iteration instead of ret += ...,
then everything works fine.

So I guess it has something to do with a possible maximum length of a
string?
Regards,

Martin


Sep 14 '06 #6

P: n/a
"Greg Young" <dr*******************@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
John ...

If I remember correctly A few of the upper bits of the length (I believe 3
but don't quote me) are used as flags for other information .. so I
believe this limits it in the MS implementation to 28 bits in length which
is substantially shorter ... of course you are quite unlikely to hit this
as the limit will tend to be the largest contiguous area of memory
available.
Won't virtual memory let you have a string that is larger than the largest
contiguous area of memory available.
Sep 14 '06 #7

P: n/a

"Allan Ebdrup" <eb****@noemail.noemailwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
| "Greg Young" <dr*******************@hotmail.comwrote in message
| news:%2****************@TK2MSFTNGP02.phx.gbl...
| John ...
| >
| If I remember correctly A few of the upper bits of the length (I believe
3
| but don't quote me) are used as flags for other information .. so I
| believe this limits it in the MS implementation to 28 bits in length
which
| is substantially shorter ... of course you are quite unlikely to hit
this
| as the limit will tend to be the largest contiguous area of memory
| available.
|
| Won't virtual memory let you have a string that is larger than the largest
| contiguous area of memory available.
|
|

No, the current CLR versions (both 32 and 64bit) limits the object size to a
maximum of 2GB. As, a System.String instance is just an array of unicode
bytes, it means that the string is limited to 1GB characters. Note that on a
32 OS, you will never be able to allocate that object size from the heap,
here the max. size of a string will be much less tha 1GB.

Willy.


Sep 14 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.