C
C#16mo ago
JJ

❔ char and character size difference

Im wandering why beteween char size having 2 byte and character size having 1byte is differ. character isn't it 2byte since char data type?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CS_Tutorial
{
internal class Program
{
static void Main(string[] args)
{
string abc = "abcdefg";
int size = Marshal.SizeOf(abc[0]);
Console.WriteLine(size);
Console.WriteLine(sizeof(char));

}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CS_Tutorial
{
internal class Program
{
static void Main(string[] args)
{
string abc = "abcdefg";
int size = Marshal.SizeOf(abc[0]);
Console.WriteLine(size);
Console.WriteLine(sizeof(char));

}
}
}
2 Replies
sibber
sibber16mo ago
Marshal.SozeOf returns the marshalled size and marshalled chars are convertes to utf8 (.net chars are utf16) min char size for utf8 is 1 bye, utf16 is 2 bytes
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server