c# - Value vs Reference -


What should I decide about the equivalent (), reference, eclipse, and == from the following results? What exactly do they produce?

  #region int integer = 1; Int integer 2 = integer; Bull reference equality = (integer == integer 2); // true boolean value avow = integer Eccles (integer 2); // true boolean value aquality plus = object Eclog (integer, integer 2); // true boolean value aquality plus = object Reference aquals (integer, integer 2); // false # field # regional # integer = integer = 1; Int integer 2 = 1; Bull reference equality = (integer == integer 2); // true boolean value avow = integer Eccles (integer 2); // true boolean value aquality plus = object Eclog (integer, integer 2); // true boolean value aquality plus = object Reference aquals (integer, integer 2); // False # Parentian # Magbul obj = New MyClass (1, "Hello"); MacCulls obz 2 = ob; Bool Reference Aquality = (obj == obj2); // True Bull Value Aquality = Obj.Aquals (OBJ2); // true boolean value aquality plus = object Ecloss (OBJ, OBJ2); // true boolean value aquality plus = object Reference AQWS (OBJ, Obj2); // True ## Regulan # Magul obj = New MyClass (1, "Hello"); Myklos Obj 2 = New Mychelle (1, "Hello"); Bool Reference Aquality = (obj == obj2); // Wrong boolean values ​​avality = obj.Equals (obj2); // Wrong boolean value equality plus = object Ecloss (obj, obj2); // Wrong boolean value equality plus = object Reference AQWS (obj, obj2); // False # Parenkin  

Hell! I understand nothing.

The reference to 1 block for me (should be wrong) == in the second block (as the references are different). And, equal in the fourth block () both should be correct (as their values ​​are the same).

The first issue of confusion is that with value type i.e. int , float , datetime , the == operator value is the similarity, with reference types, == is ( By default, see below) Reference equality. This will explain the inequality of the answer in your first two integer cases.

Second, equals () the reference to the test is not equality, the value is not equality. Therefore, when it appears that MyClass does not override equals () , which tells the inequality of the answer between your reference cases.

In addition, override to provide multiple context types, such as string , == to the value semicolon semantics, then your best The condition, unless you do not remember which types are, see the documentation.

In short:

  • Value type
    • Reference reference is not, therefore context equality is useless
    • Reference type
      • ReferenceEclock () always reference parity
      • == default The context is similar, but may be (and it often happens to override to provide value equality
      • equal () by default reference context, but the value To provide equality A override (and can often be to structure type)

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -