sizeof(T)
and T*
for reference type T
work
#8953
-
The current
However, static class Program
{
public static unsafe void Main()
{
object o = "Hello,";
object* p = &o;
Console.WriteLine(o);
*p = "world!";
Console.WriteLine(o);
Console.WriteLine(sizeof(object));
}
} Unsurprisingly, on 64-bit platforms, it outputs
or Whose bug? But it's good.Is this a bug of the compiler (which must become feature as it's been widely available), or is this a bug of the documentation? It appears to be the former case, because the ECMA version (December 2023) also doesn't allow The current behavior is entirely expected and sometimes needed. For example, // default is Sequential layout
public readonly struct ValidUtf16
{
public readonly string Value;
public ValidUtf16(string s)
{
// Somehow check s is valid UTF-16 and throw otherwise.
Value = s;
}
}
public static unsafe class ValidUtf16Extensions
{
static ValidUtf16Extensions()
{
if (sizeof(ValidUtf16) != sizeof(string))
{
throw new Exception("Code in this class relies on sizeof(ValidUtf16) == sizeof(string)");
}
}
public static ReadOnlySpan<string> AsStrings(this Span<ValidUtf16> that)
{
return MemoryMarshal.CreateReadOnlySpan(
ref Unsafe.As<ValidUtf16, string>(ref MemoryMarshal.GetReference(that)),
that.Length);
}
} In the example above, Runtime's perspectiveReading ECMA-335 (CLI) Edition 6... Of course, From II.14.4 (pointer types), the runtime indeed supports From II.15.5.4 (data type marshaling), to quote,
It suggests that the runtime is expected to see More on the docsRegarding the documentation text (in
This is logically incorrect.
SummaryCurrently, the C# compiler accepts
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The spec is not "current". It's for a quite old language version. The specialization work is left behind from language development for about 5 years. |
Beta Was this translation helpful? Give feedback.
Yes, the language design documentation is not as detailed as ECMA specification. That's why specification is a standalone work at https://github.com/dotnet/csharpstandard .
The C# language reference documentation is also updated separately. Since this feature wasn't included in What's new in C# 11, maybe it's forgotten.