Operators in MC++

  • Thread starter Thread starter 011
  • Start date Start date
0

011

Hello, All!

How can I use operators for managed objects im MC++?

String s1 = S"1234";
String s2 = S"2345";
String s3;
s3 = s1 + s2; // doesn't work

Regards,
011.

Winamp 5.0 (playing): Stratovarius - Babylon
 
The operator+ is not defined for String because String is immutable, meaning
you cannot change its value was it was created.

You need to use System.Text.StringBuilder for operations on Strings.
 
Daniel,
The operator+ is not defined for String because String is immutable, meaning
you cannot change its value was it was created.

Actually, that's completely incorrect. operator+ doesn't exist (well,
actually op_Addition() since it's a managed operator), but not for the
reasons you specify (a quick look at the contract for managed operators will
quickly reveal why)
Sounds like you're confusing it with operator+=, which certainly doesn't
exist.
You need to use System.Text.StringBuilder for operations on Strings.

Uhh, no that's incorrect again.

In 011's question, the correct answer is to use String::Concat(), instead.
 
While Tomas has already answered you, one reason you might be confused is
that in C# you can actually seem to add String objects :-)

But if you look at the IL you'll see that C# generates IL that calls
System.String::Concat
 
Hello, Tomas!
You wrote on Wed, 17 Mar 2004 19:09:40 -0500:

TRM> Actually, that's completely incorrect. operator+ doesn't
TRM> exist (well, actually op_Addition() since it's a managed
TRM> operator), but not for the reasons you specify (a quick look
TRM> at the contract for managed operators will quickly reveal
TRM> why)
TRM> Sounds like you're confusing it with operator+=, which
TRM> certainly doesn't exist.

Can we call op_Addition then?


We can use StringBuilder to improve execution speed.

TRM> In 011's question, the correct answer is to use
TRM> String::Concat(), instead.

Why are all overloads of it static?

Regards,
011.

Winamp 5.0 (not active)
 
Hello, Nishant!
You wrote on Thu, 18 Mar 2004 10:00:46 +0530:

NS> But if you look at the IL you'll see that C# generates IL that
NS> calls
NS> System.String::Concat

It is interesting why I cannot see IL code during debugging while I can see native one.

Regards,
011.

Winamp 5.0 (not active)
 
011 said:
Hello, All!

How can I use operators for managed objects im MC++?

String s1 = S"1234";
String s2 = S"2345";
String s3;

All these String objects in MC++ need to be "String *" and not just
"String". String is a __gc class and needs to be in dynamic __gc memory.
s3 = s1 + s2; // doesn't work

If your string objects are declared correctly, then you are adding pointers,
not concatentaing strings.
 
Can we call op_Addition then?

Not for String, since it doesn't exist (it's an illusion brought by the C#
compiler), but you can certainly call it explicitly on other types that do
implement it.
We can use StringBuilder to improve execution speed.

Correct, although unless you're doing a lot of concatenations, it will be
somewhat slower.
Why are all overloads of it static?
Because strings are immutable, and the designers probably thought making the
methods static made it clearer how they operated (i.e. they return a new
string instance without modifying the arguments)
 
Hello, Edward!
You wrote on Thu, 18 Mar 2004 20:04:26 -0500:

ED> All these String objects in MC++ need to be "String *" and not
ED> just "String". String is a __gc class and needs to be in
ED> dynamic __gc memory.
ED> If your string objects are declared correctly, then you are
ED> adding pointers, not concatentaing strings.

Yeah I know I've forgotten to add an asterisk. I just asked to know, is there any way to concatenate two strings using operators.

Regards,
011.

Winamp 5.0 (playing): Stratovarius - Forever Free
 
Back
Top