M
Morgan Cheng
In P/Invoke situation, If some *out* parameter is LPWSTR, I can use
string or StringBuilder. However, there is one problem about
StringBuilder. By default, its Capacity is 16. If the returned string
length is larger than 16. An ArgumentOutOfRangeExceptoin is raised.
However, if string is used, no exception.
I searched this topic. It is said that StringBuilder is preferred in
this case, because the performance is better. I don't think so. The
*out* keyword just means that the return value will be assigned to the
argument. The argument itself is un-initialized in the funciton body.
public static void foo(out string str)
{
Console.WriteLine(str);// it doesn't compile, because str is just a
symbol, not intitalized.
}
IMHO, what actually happens to string or StringBuilder only matters in
the last stage. That is CLR finally compose a LPWSTR and try to assign
to String or StringBuilder. The performance is no different for String
and StringBuilder because is only one shot. No redundant String object
is created. I don't understand why StringBuilder is recommended in
P/Invoke according to some resources.
string or StringBuilder. However, there is one problem about
StringBuilder. By default, its Capacity is 16. If the returned string
length is larger than 16. An ArgumentOutOfRangeExceptoin is raised.
However, if string is used, no exception.
I searched this topic. It is said that StringBuilder is preferred in
this case, because the performance is better. I don't think so. The
*out* keyword just means that the return value will be assigned to the
argument. The argument itself is un-initialized in the funciton body.
public static void foo(out string str)
{
Console.WriteLine(str);// it doesn't compile, because str is just a
symbol, not intitalized.
}
IMHO, what actually happens to string or StringBuilder only matters in
the last stage. That is CLR finally compose a LPWSTR and try to assign
to String or StringBuilder. The performance is no different for String
and StringBuilder because is only one shot. No redundant String object
is created. I don't understand why StringBuilder is recommended in
P/Invoke according to some resources.