using
ifadesini (potansiyel olarak) boş bir nesnede kullanmak güvenli midir?Null nesnesiyle deyimi kullanma
class Test {
IDisposable GetObject(string name) {
// returns null if not found
}
void DoSomething() {
using (IDisposable x = GetObject("invalid name")) {
if (x != null) {
// etc...
}
}
}
}
o Dispose
nesne boş değil yalnızca denilen olacağını garanti ediyor ve bir NullReferenceException
almazsınız:
Aşağıdaki örneği ele alalım?
Denemeyi öneririm ... İhtiyacınız olan kodun çoğuna sahip oldunuz – cjk
İlgili http://stackoverflow.com/questions/2513035 –