I have this code here, which is intended to allow any type of arguments:
public static void AreEqual(object expectedValue, object actualValue) {
if (expectedValue == actualValue) {
HttpContext.Current.Response.Write("Equal");
} else {
HttpContext.Current.Response.Write("Not Equal");
}
}
If I call it using a couple of ints it does not behave very well.
AreEqual(3, 3) // prints Not Equal
At the simplest level:
public static void AreEqual(object expectedValue, object actualValue) {
if (object.Equals(expectedValue,actualValue)) {
HttpContext.Current.Response.Write("Equal");
} else {
HttpContext.Current.Response.Write("Not Equal");
}
}
Or with generics (supports IEquatable<T>
):
public static void AreEqual<T>(T expectedValue, T actualValue) {
if (EqualityComparer<T>.Default.Equals(expectedValue,actualValue)) {
HttpContext.Current.Response.Write("Equal");
} else {
HttpContext.Current.Response.Write("Not Equal");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With