Problem about operator = in C#

  • Thread starter Thread starter Archer
  • Start date Start date
A

Archer

I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡­¡­¡­¡­¡­¡­¡­¡­
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be override
in c#.Thank you
 
I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡­¡­¡­¡­¡­¡­¡­¡­
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be
override in c#.Thank you

What about using System.Collections.ArrayList instead of constructing your
own List?

And if you really want your own impl: a class is always passed by
reference. so you will need a struct for your desired behaviour.
 
What about the object i created for e?
is it be released auto?And what is the difference between stuct & class in
C#?
 
What about the object i created for e?
is it be released auto?And what is the difference between stuct & class in
C#?
 
What about the object i created for e?
is it be released auto?And what is the difference between stuct &
class in C#?

All objects will be released and collected by the garbage collector.

structs are value types
classes are reference types

when you're asking such questionis it would be good if you read some basic
tutorial before you start thinking about stuff like linked lists.

http://msdn.microsoft.com/netframework/using/gettingstarted/default.aspx
 
Archer wrote: said:
¡­¡­¡­¡­¡­¡­¡­¡­
after the programme below ran,
List d=new List();
// d is a reference to a new List object, we'll call the object List #1
List e=new List();
// e is a reference to a new List object, let's call the object List #2
e.Data=4;
// The value of Data in List #2 = 5
// d is now a reference to the List #2 object (just like 'e'). The
// List #1 object is not referenced, and may now be garbage collected
d.Data=5;
// The value of Data in List #2 = 5
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do?

Don't change it? :)

It's hard to know exactly what you're after here, so it's hard to
suggest an alternative. But the other poster had a good point in
suggesting walking through a good tutorial. Object references are at
the heart of C#, and it's a good idea to have a very solid handle on
them.
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

| after the programme below ran,
| List d=new List();
d --> ListA

| List e=new List();
e --> ListB

| e.Data=4;
ListB.Data becomes 4

| d=e;
d --> ListB
e --> ListB
That is, both refer to List B now, so..

| d.Data=5;
ListB.Data becomes 5

| if I don't want the
| e.Data'value changed,what should i do?

Well first you have to ask yourself what you want to achieve with that
statement (d = e) ?

| btw:the operator = can't be override in c#.

Yeah, and you shouldn't :)

RD


Archer wrote:

| I defined my class as:
| class List
| {
| public List Next;
| public int Data;
| public List()
| {
| Data=0;
| }
| }
| ¡­¡­¡­¡­¡­¡­¡­¡­
| after the programme below ran,
| List d=new List();
| List e=new List();
| e.Data=4;
| d=e;
| d.Data=5;
| The value of e.Data became 5,What does it do?if I don't want the
| e.Data'value changed,what should i do? btw:the operator = can't be
override
| in c#.Thank you
|
|

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/m1eLwEwccQ4rWPgRAjtRAJwMluP4Pch7YTvtC9IZQxKvnp0jJQCfYLh9
VsNiSA8w9EEWh0oL/Zdq3+w=
=HzXr
-----END PGP SIGNATURE-----
 
Archer said:
I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡­¡­¡­¡­¡­¡­¡­¡­
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be override
in c#.Thank you

If you want d and e to be references to independent lists, you need to
clone the list rather than having two references to the same object.

As other posters have said, it's vital to understand how reference
types and value types work. There's some information on this in

http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html
 
Jon, please, could you give me a better explanation of the sentence below?

"Instance variables for a value type are stored in the same context as the
variable that declares the value type.
The memory slot for the instance effectively contains the slots for each
field within the instance. That means (...) that a struct variable declared
within a method will always be on the stack, whereas a struct variable which
is an instance field of a class will be on the heap"


It got a little bit confused for me. Thanks for your time.
--



---------------
Valmir Cinquini
Italian Citizenship: visit http://www.cinquini.com.br


Archer said:
I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡­¡­¡­¡­¡­¡­¡­¡­
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be override
in c#.Thank you

If you want d and e to be references to independent lists, you need to
clone the list rather than having two references to the same object.

As other posters have said, it's vital to understand how reference
types and value types work. There's some information on this in

http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html
 
Hi,
'struct' is value type and 'class' is reference type.
Thanks And Regards.
Madhanmohan S
Archer said:
What about the object i created for e?
is it be released auto?And what is the difference between stuct & class in
C#?
 
Bill McNeal said:
Jon, please, could you give me a better explanation of the sentence below?

"Instance variables for a value type are stored in the same context as the
variable that declares the value type.
The memory slot for the instance effectively contains the slots for each
field within the instance. That means (...) that a struct variable declared
within a method will always be on the stack, whereas a struct variable which
is an instance field of a class will be on the heap"


It got a little bit confused for me. Thanks for your time.

It means that if you have:

public class Foo
{
int x;

void Something()
{
int y = 5;
}
}

then the value of y is on the stack when Something is called, whereas
the value of x for any instance is stored with the rest of the
information about that instance, on the heap.

Does that help?
 
Bill McNeal said:
By the way, reading the articles you suggested, I'd like to answer one more
wuestion: What's the big deal using output parameters, if they behave like
reference parameters? Both need to have the variables assigned before their
use

No they don't - out parameters don't need to have the variables
assigned before their use (in the calling code) and they *do* need to
have values assigned before the method completes normally (ie without
an exception). The method can't read the value of the variable until it
assigns it, either.

My article (http://www.pobox.com/~skeet/csharp/parameters.html) gives a
little bit more detail on this, and an example - I suggest you play
around with the example code, changing out to ref, commenting out
assignments etc, and see what the differences are.
and both change the variable's value in the calling code if the called
function change them.

Yes.
 
Yes, it does.

By the way, reading the articles you suggested, I'd like to answer one more
wuestion: What's the big deal using output parameters, if they behave like
reference parameters? Both need to have the variables assigned before their
use, and both change the variable's value in the calling code if the called
function change them.

So why use them?

Thanks again for your time.

--



---------------
Valmir Cinquini
 
Bill McNeal said:
Ok Jon, thanks again for your reply. I have already played a little with the
source code provided by your article, and i understood all about outpu
parameters (it was easier than where ref and value parameters differs)

I just couldnt figure out which benefits I can get using output parameters
instead of reference parameters. Just when I dont have a value to assign to
a variable before pass it to a function? So what? I can assign some "dummy"
value and pass it to a function as a ref parameter.

Please, if there is something I dont know, them let me know.

Well, a few things:

o Assigning a dummy parameter value is ugly - unless you specifically
document that you don't expect the value to get used, the maintainer
of the code may well try to work out why you've used that particular
value, and what its significance is.

o The fact that it's an out parameter means that the caller knows that
the value of the variable really doesn't matter at all, and enforces
that for the called method (by treating it as unassigned to start
with).

o The fact that it's an out parameter forces the called method to
assign a value to it before leaving the method, which is as helpful
as the compiler error message of "not all code paths return a
value" - by declaring the parameter as "out" you're basically saying
that you know you definitely want to set the value before you return,
and the compiler will help to make sure that you do.
 
Bill McNeal said:
Well, thanks again for your good explanation. IMHO I never faced a situation
where I needed an output parameters.

Oh absolutely - I very, very rarely use either output *or* ref
parameters. I think they're generally a bad sign, and whenever I
consider using them, I think about whether a restructuring would
actually be better.
But the second and third points you
describe really gave me some good reasons to use output parameters.

Goodo :)
Despite the dummy value turns the code ugly....well, usually we finished
being "parents"of an application for the rest of our lives while working for
a company. Unless you work for a very organized company, once you dont work
for them anymore, other person will try understand what you code and
probabilly, will change to their "code style".

Unless you're the only programmer in the company, that seems unlikely
to me. I often need to look at code written by other members of the
company, especially when the product isn't developed by a single
person. I guess different companies work in different ways though. I
still don't like the idea of assigning values which will never be read
though :)
Well, thanks again for you patience replying me.

No problem :)
 
Ok Jon, thanks again for your reply. I have already played a little with the
source code provided by your article, and i understood all about outpu
parameters (it was easier than where ref and value parameters differs)

I just couldnt figure out which benefits I can get using output parameters
instead of reference parameters. Just when I dont have a value to assign to
a variable before pass it to a function? So what? I can assign some "dummy"
value and pass it to a function as a ref parameter.

Please, if there is something I dont know, them let me know.

Thanks again.

Valmir
 
Well, thanks again for your good explanation. IMHO I never faced a situation
where I needed an output parameters. But the second and third points you
describe really gave me some good reasons to use output parameters.

Despite the dummy value turns the code ugly....well, usually we finished
being "parents"of an application for the rest of our lives while working for
a company. Unless you work for a very organized company, once you dont work
for them anymore, other person will try understand what you code and
probabilly, will change to their "code style".


Well, thanks again for you patience replying me.

--



---------------
Valmir Cinquini

Cidadania Italiana: visite http://www.cinquini.com.br
Banda larga de 1º mundo: Visite http://speedy.bravehost.com/speedy.htm
Lista MS-SQL: http://br.groups.yahoo.com/group/mssql-pt/join
Lista C#: http://br.groups.yahoo.com/group/csharp-pt/join
 
Back
Top