How to override the operator =

  • Thread starter Thread starter David Lau
  • Start date Start date
You can override the implicit cast - operator. (see keyword implicit in
MSDN)
 
Simon Trew said:
If the poster is in China (.cn address) the clock may be correct.

Then their timezone isn't.

The original post had a date header of:

Date: Tue, 19 Aug 2003 11:15:18 -0700

That can't possibly be right, as that time (18:15 GMT) hasn't yet
happened.
 
Ashish said:
Code in C++ :-) or just override equals in C#
Ashish

Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be another
solution.

Nathan
 
You cannot override/overload or otherwise modify
the assignment (=) or new operators in .NET.

There is specific logic that .NET uses for assignment
that is required for the memory management and
garbage collection.

I think what you may want is to override the
implicit and explicit cast operator.

For example:

MyFancyNumberClass mfnc = new MyFancyNumberClass():

// implicit cast
int foo = mfnc;

If you wrote MyFancyNumberClass and you wanted
people to copy it to an int, you'd use the
implicit cast operator to convert mfnc to an
integer.

You can also override explicit casts:
int foo = (int) mfnc;

-c
 
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;
 
OK ,Thanks a lot .


Chad Myers said:
You cannot override/overload or otherwise modify
the assignment (=) or new operators in .NET.

There is specific logic that .NET uses for assignment
that is required for the memory management and
garbage collection.

I think what you may want is to override the
implicit and explicit cast operator.

For example:

MyFancyNumberClass mfnc = new MyFancyNumberClass():

// implicit cast
int foo = mfnc;

If you wrote MyFancyNumberClass and you wanted
people to copy it to an int, you'd use the
implicit cast operator to convert mfnc to an
integer.

You can also override explicit casts:
int foo = (int) mfnc;

-c
 
Back
Top