471,357 Members | 1,118 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

left add unary operator ( += )


Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

....writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #1
8 3679
On 2005-01-29, Elementary Penguin <si*****@hare.krishna> wrote:

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?


No.

--
Tom Shelton
Jul 21 '05 #2
Tom Shelton wrote:
No.


Thanks, that's what I thought.

So I'm just going to do:

s1 = "A";
s1 = "B" + s1;

--
Texeme
http://texeme.com

Jul 21 '05 #3
No. And no, you can't overload "=+" because "=+" isn't a legal operator.

You can do

string s2 = "B";
s2 += s1;

instead.

-vJ

"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:tY***************@newsread3.news.pas.earthlin k.net...

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #4
No there is no =+ operator
No you cannot create a brand new operator
Not in C# anyway
Cheers,

"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:tY***************@newsread3.news.pas.earthlin k.net...

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #5

AFAIK there is no operator like this.
You'd need to create a new string class, just to expose this operator. Why
not just write
s1 = "B" + s1;

Alternatively you could create a new class with the operator

public class XString : System.String // not sure you can inherit from string
though?
{
public static void operator =+ ( XString xs1, XString xs2 )
{
xs1 = xs2 + xs1;
}
}

The final thing on this is that for string concatenation, it's better to use
the System.Text.StringBuilder object.
http://www.c-sharpcorner.com/Code/20...uilderComp.asp

Hope that helps.

Dan.

"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:tY***************@newsread3.news.pas.earthlin k.net...

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #6
Dan Bass wrote:
public class XString : System.String // not sure you can inherit from string
though?
{
public static void operator =+ ( XString xs1, XString xs2 )
{
xs1 = xs2 + xs1;
}
}


This won't work because:
1) String is a sealed class
2) The =+ operator will result in a "Overloadable unary operator
expected" compiler error.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Jul 21 '05 #7
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:

<snip>
The final thing on this is that for string concatenation, it's better to use
the System.Text.StringBuilder object.
http://www.c-sharpcorner.com/Code/20...uilderComp.asp


That entirely depends on the context. If you actually need all of the
intermediate strings (ie the result of each concatenation) then you're
better off using straight string concatenation. Similarly, for a small
number of concatenations, the copy hit of string concatenation can
often be smaller than the hit of creating a StringBuilder.

Where StringBuilder really pays off is where you have string
concatenation in a loop.

You might find this article useful - it's about Java's StringBuffer,
but the principles are exactly the same:
http://www.pobox.com/~skeet/java/stringbuffer.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8
Someone please correct me if I'm wrong.

As of CSharp 2003 it not possible to define a new operator ie; =+ also some
existing operators cannot be overloaded ie; +=.

Courtesy MSDN Section 7.2.2 Operator Overloading

<<<<<<<< Start Here>>>>>>>>>>

The overloadable unary operators are:

+ - ! ~ ++ -- true false
Although true and false are not used explicitly in expressions, they are
considered operators because they are invoked in several expression
contexts: Boolean expressions (Section 7.16) and expressions involving the
conditional (Section 7.12), and conditional logical operators (Section
7.11).

The overloadable binary operators are:

+ - * / % & | ^ << >> == != > < >= <=
Only the operators listed above can be overloaded. In particular, it is not
possible to overload member access, method invocation, or the =, &&, ||, ?:,
checked, unchecked, new, typeof, as, and is operators.

<<<<<<<< End Here>>>>>>>>>>

So you could choose from any of the above but you might find then a little
meaningless to the next programmer, who might just happen to be you in 6
months time.
My suggestion define a method called EqualsPlus until such a time you can
create custom defined operators.
Regards

Ian
"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:tY***************@newsread3.news.pas.earthlin k.net...

Is there a left add unary operator ( or do I have to overload one ) ?

For example:

string s1 = "A";

s1 += "B";

Console.WriteLine( s1 )

...writes: AB

But, say I want BA.

Is there some kind of

s1 =+ B;

I can overload an operator, but can I create a brand new operator ?

--
Texeme
http://texeme.com

Jul 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Carlos Ribeiro | last post: by
7 posts views Thread by John J | last post: by
5 posts views Thread by Ruben Campos | last post: by
2 posts views Thread by Javier Estrada | last post: by
8 posts views Thread by Elementary Penguin | last post: by
13 posts views Thread by Marc | last post: by
6 posts views Thread by Matthew Cook | last post: by
28 posts views Thread by dspfun | last post: by
16 posts views Thread by JoseMariaSola | last post: by

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.