473,382 Members | 1,396 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,382 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 3789
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't...
7
by: John J | last post by:
To write a unary operator- operation that returns the negative of a value is it just as simple as subtracting the value from itself? ie. { return (value - value); } Thanks
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
2
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
8
by: Elementary Penguin | last post by:
Is there a left add unary operator ( or do I have to overload one ) ? For example: string s1 = "A"; s1 += "B"; Console.WriteLine( s1 )
13
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even...
6
by: Matthew Cook | last post by:
I would like to overload the unary minus operator so that I can negate an instance of a class and pass that instance to a function without creating an explicit temporary variable. Here is an...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
16
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.