2
Answers

Passing Objects by Value

Ask a question
Administrator

Administrator

21y
1.6k
1
I am trying to work through an example in a book and have run across something that just doesn't make sense. In the method listed below, I am passing in a object of type Fraction. public override bool Equals(object o) { if (!(o is Fraction)) { return false; } // Console.WriteLine(o.GetType()); return this ==(Fraction) o; } What I don't understand is why the cast in the last line. If I take the cast out I get the following error: 'Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'ConsoleApplication18.Fraction' If I uncomment the Console.Writeline it tells me 'o' is of type Fraction. In addition, the error message makes it sound like if I cast to Fraction then the value comparison will be ok. But Fraction is an object and therefore a reference type not a value type. Can someone explain this to me.

Answers (2)