473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Interop: String or StringBuilder

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.

Sep 25 '06 #1
3 8689
Hi,

Check www.pinvoke.net you will have a lot of examples (even maybe yours)

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
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.

Sep 25 '06 #2

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
| 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.
|

This same question is answered in your previous post.
No one ever said that a StringBuilder is faster than a String when passed as
argument to another method (and in your case to an unmanaged function using
the managed/native interop layer).
What you have to pass to unmanaged code depends on the semantics, an out
argument doesn't require a StringBuilder, all you need to pass is an
(initialized or uninitialize, doesn't matter) String variable, the marshaler
(default implicit behavior) will correctly marshal the unmanaged buffer to a
String instance and return the reference into the variable.
Whenever you pass a String requiring In/Out semantics, you better pass a
StringBulder. But here again you need to make sure the StringBuilder is
large enough to accommodate the returned string.

Willy.



Sep 25 '06 #3
Hi Morgan,

"Morgan Cheng" <mo************@gmail.comschrieb im Newsbeitrag
news:11**********************@d34g2000cwd.googlegr oups.com...
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.
the performance of the called function certainly wont change, but the
marshalling of the
parameters could perform different. But depending on what the function does
and how
often it is called the difference wouldn't matter much.
Only if the function itself runs very short and is called very often it
would be a performance issue.
In case of doubt you should performance test it for your application.

hth
Sep 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Webgour | last post by:
How to go from string "abc" to string "a|b|c"?
0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
3
by: todorov-fkt | last post by:
Hello Provided the following code: string temp = "Short string"; // 12 chars StringBuilder sb = new StringBuilder(); sb.Append(temp, 0, 30); How many characters is the string in sb? 12 or is...
5
by: ad | last post by:
I have a string like string myString="dog,cat,dog,tiger" I want to delete the duplication part, and make myString to "dog,cat,tiger" How can I do that?
12
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
7
by: KH | last post by:
API question... Why doesn't StringBuilder have IndexOf and other similar methods like String? I can't think of a good reason off the top of my head. Easy to write helper functions to do the...
5
by: Jason | last post by:
I am trying to retrieve string data from a c++ dll: extern "C" __declspec( dllexport ) const char * getbagstr(); extern "C" __declspec( dllexport ) const char * getbagstr() { const char * buff...
5
by: sherifffruitfly | last post by:
Hi all, I just wrote this up real quick, and it seems to work with a bunch of input strings (with and without spaces) except for the one in the code below: string input = "this is a string";...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.