Sunday, March 21, 2010

Some strange optimization of the C# :D

Sometimes optimization mechanism build in the compilator is able to do some strange things. For example when the following code is concerned we think than reference var1 is not equal the var2 reference.

string var1 = "text";
string var2 = "text";

bool condition = object.ReferenceEquals(var1, var2);


But the optimizer of the C# "thought" that it is not needed to create some new reference on the management heap and it uses the local one.
When we make a little change in the previous code, for example modify the second string by adding the "t" letter at the end of it after its initialization

string var1 = "text";
string var2 = "tex";
var2 += "t";

bool condition = object.ReferenceEquals(var1, var2);

then the references will not be eqal.

This could be a cause of a huge set of problems in the real programming world, so take care about it :)

1 comment:

Waldemar Dacko said...

Chłopie, może w jakimś cywilizowanym języku napiszesz do mnie!!!

Search This Blog