SORU
16 EYLÜL 2008, Salı


Nasıl'==' sonsuz özyineleme olmadan operatör aşırı bir boşluk için kontrol edebilirim?

Aşağıdaki == operatör yöntemi aşırı sonsuz özyineleme neden olur

    Foo foo1 = null;
    Foo foo2 = new Foo();
    Assert.IsFalse(foo1 == foo2);

    public static bool operator ==(Foo foo1, Foo foo2) {
        if (foo1 == null) return foo2 == null;
        return foo1.Equals(foo2);
    }

Null nasıl kontrol edebilirim?

CEVAP
16 EYLÜL 2008, Salı


Kullanım ReferenceEquals:

Foo foo1 = null;
Foo foo2 = new Foo();
Assert.IsFalse(foo1 == foo2);

public static bool operator ==(Foo foo1, Foo foo2) {
    if (object.ReferenceEquals(null, foo1))
        return object.ReferenceEquals(null, foo2);
    return foo1.Equals(foo2);
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BachelorsPadTv

    BachelorsPad

    17 Ocak 2012
  • glowpinkstah

    glowpinkstah

    16 Mayıs 2006
  • iNCH

    iNCH

    20 Temmuz 2009