kamal
kamal

Reputation: 759

Char pointer equivalent in c#

I am converting c++ code into c#, and char in c++ takes 8 bits while in c# takes 16 bits. I don't know about char*, So

What is the equivalent of char * in C#, Do i use byte[] or [MarshalAs(UnmanagedType.LPStr)] StringBuilder and also tell me whether the equivalent of char from C++ to C# is byte or string?

Upvotes: 0

Views: 9305

Answers (3)

Dirk
Dirk

Reputation: 10968

For an input parameter it could be a string or a byte[], depending on the meaning of the parameter. If it represents a sequence of characters then use string. If the parameter is a buffer to some arbitrary data then it's most likely a byte[].

However in C/C++ a char * can also be an output parameter, such as in the sprintf function. In that case a StringBuilder or a byte[] would be the equivalent types, depending again on the meaning of the parameter.

With regards the char datatype in C#, please keep in mind that a char in C# means character, whereras the meaning the C/C++ is closer to that of a byte in C#.

Upvotes: 2

0x90
0x90

Reputation: 41012

It depends:

  1. char *ptr should be replaced with string if it acts as a string container (like commonly used in c).
  2. If what matters is the value in it and to bound its range in-between [-127,128] use char[] or byte[] e.g. char[] array1 = { 'b', 'c', 'a' };

Upvotes: 0

Dolo
Dolo

Reputation: 976

Can't understand the question correctly but C# too have Char type http://www.dotnetperls.com/char

Upvotes: 0

Related Questions