left add unary operator ( += )

  • Thread starter Thread starter Elementary Penguin
  • Start date Start date
E

Elementary Penguin

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 ?
 
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.
 
No. And no, you can't overload "=+" because "=+" isn't a legal operator.

You can do

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

instead.

-vJ
 
No there is no =+ operator
No you cannot create a brand new operator
Not in C# anyway


Cheers,
 
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/2002/Oct/StringBuilderComp.asp

Hope that helps.

Dan.
 
Dan said:
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/
 
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:

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/2002/Oct/StringBuilderComp.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
 
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
 
Back
Top