String implementation ?

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

how is a String implemented in .NET :

as in VB.NET : using a length-prefix located in front of the actual
characters
or
as in C : using '\0' (backslash null)

thnx
Chris
 
Chris,

It probably uses a length-prefix somewhere. However, because the object
is managed, you won't be able to access it (except indirectly through the
Length property).

Also, since strings are immutable, if you modified this prefix, it could
have some very bad ramifications.

Hope this helps.
 
Mattias Sjögren said:
They both contain a terminating null character and has the length
stored.

I'm not sure that it does have a terminating NUL (*), however, even if
it does, it's irrelevant, as String allows internal NUL characters.


(*) "NUL" = a single character of value Zero.
"Null" = an pointer/reference value indicating it is unassigned.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
James Curran said:
I'm not sure that it does have a terminating NUL (*), however, even if
it does, it's irrelevant, as String allows internal NUL characters.

It's far from irrelevant - it's vital for interop. If strings weren't
nul/null-terminated, you wouldn't be able to pass them to native code
which expected them to be nul/null-terminated.
(*) "NUL" = a single character of value Zero.

I think it depends on where you're getting your terminology from. For
instance, looking at http://www.unicode.org/charts/PDF/U0000.pdf
the box in the character chart has "NUL" in it, but the description is
"NULL".
"Null" = an pointer/reference value indicating it is unassigned.

There's a difference between "unassigned" and "assigned a null
reference".
 
It's far from irrelevant - it's vital for interop. If strings weren't
nul/null-terminated, you wouldn't be able to pass them to native code
which expected them to be nul/null-terminated.

By the time it's reached interop, it's already gone through marshalling,
where it could have added the nul-terminator (at the same time it removes
the length).

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
James Curran said:
By the time it's reached interop, it's already gone through marshalling,
where it could have added the nul-terminator (at the same time it removes
the length).

Well, it can do that in cases where the marshalling is copying the
data. When it's not copying the data, the null already needs to be
there, surely - or there at least needs to be enough space for it. (I
don't believe the length is removed, so much as the pointer passed to
unmanaged code is the address of the first character of the string.)

Maybe I'm wrong and the contents is always copied though. It seems a
shame if this is the case - and I'm pretty sure that the null always
*is* there. The space taken up by the character data is always a
multiple of 4 bytes, and the way I've seen it expand suggests that
there's always at least *room* for the null.
 
Back
Top