本文整理汇总了C#中System.Object.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# Object.Equals方法的具体用法?C# Object.Equals怎么用?C# Object.Equals使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Object
的用法示例。
在下文中一共展示了Object.Equals方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: JScriptIn
//"x in y" should return true whenever "for (var z in y) if (z === x) return true" returns true
public static bool JScriptIn(Object v1, Object v2){
bool result = false;
if (v2 is ScriptObject)
return !(((ScriptObject)v2).GetMemberValue(Convert.ToString(v1)) is Missing);
else if (v2 is Array){
Array arr = (Array)v2;
double d = Convert.ToNumber(v1);
int i = (int)d;
return d == i && arr.GetLowerBound(0) <= i && i <= arr.GetUpperBound(0);
}else if (v2 is IEnumerable){
if (v1 == null) return false;
//Do not enumerate when a direct lookup is available
if (v2 is IDictionary)
return ((IDictionary)v2).Contains(v1);
if (v2 is IExpando){
MemberInfo[] members = ((IReflect)v2).GetMember(Convert.ToString(v1), BindingFlags.Instance|BindingFlags.DeclaredOnly|BindingFlags.Public);
return members.Length > 0;
}
IEnumerator enu = ((IEnumerable)v2).GetEnumerator();
while (!result && enu.MoveNext())
if (v1.Equals(enu.Current)) return true;
}else if (v2 is IEnumerator){
if (v1 == null) return false;
IEnumerator enu = (IEnumerator)v2;
while (!result && enu.MoveNext())
if (v1.Equals(enu.Current)) return true;
}
throw new JScriptException(JSError.ObjectExpected);
}
示例2: AssertEqualsHashCode
public static void AssertEqualsHashCode(Object o, Object o2)
{
if (o.Equals(o2)) {
if (!o2.Equals(o)) {
Assert.Fail(
String.Empty + o + " equals " + o2 + " but not vice versa");
}
// Test for the guarantee that equal objects
// must have equal hash codes
if (o2.GetHashCode() != o.GetHashCode()) {
// Don't use Assert.AreEqual directly because it has
// quite a lot of overhead
Assert.Fail(
String.Empty + o + " and " + o2 + " don't have equal hash codes");
}
} else {
if (o2.Equals(o)) {
Assert.Fail(String.Empty + o + " does not equal " + o2 +
" but not vice versa");
}
// At least check that GetHashCode doesn't throw
try {
o.GetHashCode();
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
o2.GetHashCode();
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}
}
示例3: getPatientName
public String getPatientName(Object markerID)
{
if (markerID.Equals("PatientMarkerConfig.txt"))
return "Smith, John";
else if (markerID.Equals(2))
return "Doe, Jane";
return "No Patient";
}
示例4: Convert
public object Convert(Object value , Type targetType , Object parameter , System.Globalization.CultureInfo culture)
{
if (value.Equals("儿童"))
{
return new SolidColorBrush(Colors.Yellow);
}
if (value.Equals("成人"))
{
return new SolidColorBrush(Colors.Orange);
}
else
return new SolidColorBrush(Colors.Gray);
}
示例5: Main
static void Main(string[] args)
{
Object o = new Object();
Object p = new Object();
Object q = o;
Console.WriteLine(o.Equals(p));
Console.WriteLine(o.Equals(q));
Console.WriteLine(Object.Equals(p, q));
Console.ReadKey();
}
示例6: AreObjectsEqual
public static bool AreObjectsEqual(Object x, Object y)
{
if (x == y)
return true;
if (x is ArrayList)
{
if (!(y is ArrayList))
return false;
ArrayList a = x as ArrayList;
ArrayList b = y as ArrayList;
if (a.Count != b.Count)
{
return false;
}
else
{
for (int i=0; i < a.Count; ++i)
{
if (!AreObjectsEqual(a[i], b[i]))
return false;
}
return true;
}
}
else
{
return x.Equals(y);
}
}
示例7: AssertArgumentNotEquals
protected void AssertArgumentNotEquals(Object anObject1, Object anObject2, String aMessage)
{
if (anObject1.Equals(anObject2))
{
throw new ArgumentException(aMessage);
}
}
示例8: checkEqualsAndHashCodeMethods
private static void checkEqualsAndHashCodeMethods(Object lhs, Object rhs,
bool expectedResult)
{
if ((lhs == null) && (rhs == null))
{
assertTrue(
"Your check is dubious...why would you expect null != null?",
expectedResult);
return;
}
if ((lhs == null) || (rhs == null))
{
assertFalse(
"Your check is dubious...why would you expect an object "
+ "to be equal to null?", expectedResult);
}
if (lhs != null)
{
assertEquals(expectedResult, lhs.Equals(rhs));
}
if (rhs != null)
{
assertEquals(expectedResult, rhs.Equals(lhs));
}
if (expectedResult)
{
var hashMessage =
"hashCode() values for equal objects should be the same";
assertTrue(hashMessage, lhs.GetHashCode() == rhs.GetHashCode());
}
}
示例9: IsEquals
public static void IsEquals(Object expectedValue, Object actualValue, string message)
{
if (actualValue.Equals(expectedValue))
return;
string s = message != null ? ": " + message : String.Empty;
string format = String.Format("Expected {0} but encountered {1}{2}", expectedValue, actualValue, s);
throw new AssertionFailedException(format);
}
示例10: assertEqual
public virtual void assertEqual(Object result, Object expecting)
{
if (result == null && expecting != null)
{
throw new FailedAssertionException("expecting \"" + expecting + "\"; found null");
}
assertTrue(result.Equals(expecting), "expecting \"" + expecting + "\"; found \"" + result + "\"");
}
示例11: AreEqual
public static void AreEqual(Object a, Object b, string message, bool launchDebugger)
{
if (a.Equals(b) == false)
{
if (launchDebugger)
DebugUtil.LaunchDebugger();
throw new Exception(message + ", '" + a.ToString() + "' != '" + b.ToString() + "'");
}
}
示例12: Equals
/// <include file='doc\Object.uex' path='docs/doc[@for="Object.Equals1"]/*' />
public static bool Equals(Object objA, Object objB) {
if (objA==objB) {
return true;
}
if (objA==null || objB==null) {
return false;
}
return objA.Equals(objB);
}
示例13: Equals
protected new bool Equals(Object o1, Object o2) {
if (o1 == null) {
return o2 == null;
} else if (o2 == null) {
return false;
} else {
return o1.Equals(o2);
}
}
示例14: Equals
public new bool Equals(Object x, Object y)
{
if (x != null)
{
var tObj = x as IStructuralEquatable;
return tObj != null ? tObj.Equals(y, this) : x.Equals(y);
}
return y == null;
}
示例15: TestEquals1
public void TestEquals1() {
{
Object x = new Object();
Object y = new Object();
Assert("Object should equal itself",
x.Equals(x));
Assert("object should not equal null",
!x.Equals(null));
Assert("Different objects should not equal 1",
!x.Equals(y));
Assert("Different objects should not equal 2",
!y.Equals(x));
}
{
double x = Double.NaN;
double y = Double.NaN;
Assert("NaNs should always equal each other",
((Object)x).Equals(y));
}
}