Coding man
Coding man

Reputation: 967

Why Marshal.Sizeof does not work for strings?

string s = "hello";
Console.WriteLine(Marshal.SizeOf(s)); // gives error

Marshal.Sizeof works for other data-types like int, char etc. but does not works for string datatype. Any specific reason for this?

Upvotes: 1

Views: 771

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

String is reference type (class) not value type (struct).

Marshal.SizeOf

This method accepts an instance of a structure, which can be a reference type or a boxed value type. The layout must be sequential or explicit.

Upvotes: 5

Related Questions