C#: What's the behavior of the fixed statement on empty strings? -
this document, part of c# language spec, says behavior of fixed
in c# implementation-defined if used on null/empty array reference. quote it:
an expression of array-type elements of unmanaged type t, provided type t* implicitly convertible pointer type given in fixed statement. in case, initializer computes address of first element in array, , entire array guaranteed remain @ fixed address duration of fixed statement. the behavior of fixed statement implementation-defined if array expression null or if array has 0 elements.
however, not make same claim empty strings, saying behavior isn't defined if string null. here's next paragraph detailing how works strings:
an expression of type string, provided type char* implicitly convertible pointer type given in fixed statement. in case, initializer computes address of first character in string, , entire string guaranteed remain @ fixed address duration of fixed statement. the behavior of fixed statement implementation-defined if string expression null.
so if i'm reading correctly, means behavior defined empty strings, right? happens, then, if like
fixed (char* pch = string.empty) { console.writeline((int)*pch); }
? guaranteed print out 0, since strings in .net null-terminated? ecma 335 implementations (e.g. microsoft's clr, mono) require strings null-terminated?
thanks.
yes, it's guaranteed print out 0 due later bit of 18.6:
a
char*
value produced fixing string instance points null-terminated string. within fixed statement obtains pointer p string instance s, pointer values rangingp
p + s.length - 1
represent addresses of characters in string, , pointer valuep + s.length
points null character (the character value '\0').
i can't i've tried on mono, non-compliance issue if case. same text occurs in ecma standard in ms specification. (clause 27.6 in c# 2 edition; looks it'll 24.7 in c# 5 edition.)
Comments
Post a Comment