Last active
August 29, 2015 14:10
-
-
Save brenoferreira/46487043c5a42a05a6d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//use read-only properties | |
class MyClass { | |
public int Prop1 { get; private set; } | |
public float Prop2 { get; private set; } | |
} | |
//GENERATED | |
class MyClass { | |
public int Prop1 { get; private set; } | |
public float Prop2 { get; private set; } | |
public bool Equals(MyClass other) | |
{ | |
if(other == null) {return false; } | |
return other.Prop1 == Prop1 && other.Prop2 == Prop2; | |
} | |
public bool Equals(Object other) { | |
if(other == null) { return false; } | |
var obj = other as MyClass; | |
if(obj == null) { return false; } | |
return Equals(obj); | |
} | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
int hash = (int) 2166136261; | |
hash = hash * 16777619 ^ Prop1.GetHashCode(); | |
hash = hash * 16777619 ^ Prop2.GetHashCode(); | |
return hash; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Do not use mutable state | |
class MyClass { | |
public int Prop1 { get; private set; } | |
public float Prop2 { get; private set; } | |
public string Prop3 { get; set; } | |
} | |
//GENERATED | |
class MyClass { | |
public int Prop1 { get; private set; } | |
public float Prop2 { get; private set; } | |
public string Prop3 { get; set; } | |
public bool Equals(MyClass other) | |
{ | |
if(other == null) {return false; } | |
return other.Prop1 == Prop1 && other.Prop2 == Prop2; | |
} | |
public bool Equals(Object other) { | |
if(other == null) { return false; } | |
var obj = other as MyClass; | |
if(obj == null) { return false; } | |
return Equals(obj); | |
} | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
int hash = (int) 2166136261; | |
hash = hash * 16777619 ^ Prop1.GetHashCode(); | |
hash = hash * 16777619 ^ Prop2.GetHashCode(); | |
return hash; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Even though Prop3 is read only, it's a reference type | |
//Reference types might have mutable state. | |
//One exception is string of course | |
class MyClass { | |
public int Prop1 { get; private set; } | |
public float Prop2 { get; private set; } | |
public int[] Prop3 { get; private set; } | |
} | |
//GENERATED | |
class MyClass { | |
public int Prop1 { get; private set; } | |
public float Prop2 { get; private set; } | |
public int[] Prop3 { get; private set; } | |
public bool Equals(MyClass other) | |
{ | |
if(other == null) {return false; } | |
return other.Prop1 == Prop1 && other.Prop2 == Prop2; | |
} | |
public bool Equals(Object other) { | |
if(other == null) { return false; } | |
var obj = other as MyClass; | |
if(obj == null) { return false; } | |
return Equals(obj); | |
} | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
int hash = (int) 2166136261; | |
hash = hash * 16777619 ^ Prop1.GetHashCode(); | |
hash = hash * 16777619 ^ Prop2.GetHashCode(); | |
return hash; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Checks for null strings in GetHashCode | |
class MyClass { | |
public int Prop1 { get; private set; } | |
public float Prop2 { get; private set; } | |
public string Prop3 { get; private set; } | |
} | |
//GENERATED | |
class MyClass { | |
public int Prop1 { get; private set; } | |
public float Prop2 { get; private set; } | |
public string Prop3 { get; private set; } | |
public bool Equals(MyClass other) | |
{ | |
if(other == null) {return false; } | |
return other.Prop1 == Prop1 && other.Prop2 == Prop2 && other.Prop3 == Prop3; | |
} | |
public bool Equals(Object other) { | |
if(other == null) { return false; } | |
var obj = other as MyClass; | |
if(obj == null) { return false; } | |
return Equals(obj); | |
} | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
int hash = (int) 2166136261; | |
hash = hash * 16777619 ^ Prop1.GetHashCode(); | |
hash = hash * 16777619 ^ Prop2.GetHashCode(); | |
hash = Prop3 == null ? hash : hash * 16777619 ^ Prop3.GetHashCode(); | |
return hash; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//If MyClass only contains mutable state, do not override GetHashCode() | |
class MyClass { | |
public int Prop1 { get; set; } | |
public float Prop2 { get; set; } | |
public string Prop3 { get; set; } | |
} | |
//GENERATED | |
class MyClass { | |
public int Prop1 { get; set; } | |
public float Prop2 { get; set; } | |
public string Prop3 { get; set; } | |
public bool Equals(MyClass other) | |
{ | |
if(other == null) {return false; } | |
return other.Prop1 == Prop1 && other.Prop2 == Prop2 && other.Prop3 == Prop3; | |
} | |
public bool Equals(Object other) { | |
if(other == null) { return false; } | |
var obj = other as MyClass; | |
if(obj == null) { return false; } | |
return Equals(obj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment