S
S
SkiF EDC2016-07-26 00:42:35
Arrays
SkiF EDC, 2016-07-26 00:42:35

How to overload assignment operator, c#?

Hello, I read on a couple of forums that it is impossible to overload this operator. How to solve this problem? '

{
    public static void operator = (Person p, Person p1);
}`

It's just that if you use ArrayList.Colone(), then by changing the new collection I change the original one, how can this be avoided? Thanks for your attention, tell me where to dig.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2016-07-26
@SkifEDC

From a C# perspective, what you want is not what you want. Unlike C++, where operator overloading makes it possible to generate individual behavior for each user-defined type, C# has built two basic behaviors for you - a reference type and a value type - and prompts you to choose one of them.
You need to reformulate the task into a more general one, and understand what you really want to do, and why you need to redefine the assignment. Judging by the name of the class (Person), you are clearly doing something wrong with it.

D
Dmitry Karataev, 2016-07-27
@Krawler

Isn't this a mix of assignment and copy constructor? The author wants behavior in the spirit of:

Person p1 = new Person( ...);
Person p = new Person(p1);

Or am I misunderstanding the question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question