2017-09-07 113 views
9

kutulu int için farklı pencerelerde farklı çalışır. Kodunuzda belirtilen object boxedInt = 0, hem is object hem de is int, Visual Studio'nun Anlık Penceresinde false döndürür. Bu bir böcek değil mi?"is" operatörü, Kutulu değerler

Kodu:

int normalInt = 0; 

Debug.WriteLine(normalInt.GetType().FullName); // System.Int32 
Debug.WriteLine(normalInt is object);   // true 
Debug.WriteLine(normalInt is int);    // true 
Debug.WriteLine(normalInt is System.Int32); // true 

object boxedInt = 0; 

Debug.WriteLine(boxedInt.GetType().FullName); // System.Int32 
Debug.WriteLine(boxedInt is object);   // true 
Debug.WriteLine(boxedInt is int);    // true 
Debug.WriteLine(boxedInt is System.Int32); // true 

Hemen Penceresi:

normalInt.GetType().FullName 
"System.Int32" 
normalInt is object 
true 
normalInt is int 
true 
normalInt is System.Int32 
true 

boxedInt.GetType().FullName 
"System.Int32" 
boxedInt is object 
false           // WTF? 
boxedInt is int 
false           // WTF? 
boxedInt is System.Int32 
false           // WTF? 

object boxedInt2 = 0; 
Expression has been evaluated and has no value 
boxedInt2.GetType().FullName 
"System.Int32" 
boxedInt2 is object 
true 
boxedInt2 is int 
true 
boxedInt2 is System.Int32 
true 

Microsoft Visual Studio Enterprise 2017
Sürüm 15.3.3
VisualStudio.15.Release/15.3.3 + 26.730,12

Microsoft .NET F
Versiyon ramework 4.7.02046

Visual C# 2017 00369-60000-00001-AA135


Ekran Watch pencere ile: Araçlar-> Seçenek-> ayıklama altında

+0

Evet. Örneğin, yukarıdaki koddan sonra bir kesme noktası ayarlarken. –

+0

Sanırım yapmanız gereken - boxedInt == typeof (object); ' –

+3

@SouvikGhosh' boxedInt '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' 'hiçbir zaman, bu asla doğru olmayacaktır. – Servy

cevap

1

, lütfen "Eski C# ve VB ifade değerlendiricilerini kullanın" seçeneğini etkinleştirin, tekrar hata ayıklayın.

enter image description here

Güncelleme:

mesele burada bildirilmiştir:

https://developercommunity.visualstudio.com/content/problem/31283/and-operation-of-boolean-is-wrong.html

+0

Gerçekten işe yaradığı için bu yararlı bir cevaptır. Ancak, neden mirassızlıktaki (yani daha iyi olması gereken) modda gözlemlenen davranışı ve bunun bir hata olup olmadığını sormaz. Buna oy veriyorum ama doğru cevap olarak kabul etmiyorum. –

+0

@Marco Eckstein, Bu sorunu bildirip diğer üyelerle tartışacağım, en yeni bilgiler alırsam, burada paylaşacağım. –

+0

@Marco Eckstein, Benzer sorun burada bildirilmiştir: https://developercommunity.visualstudio.com/content/problem/31283/and-operation-of-boolean-is-wrong.html, ürün ekibi üyeleri bunu takip edecek Ayrıca yorumunuzu orada paylaşabilirsiniz. –