2017-09-07 21 views
9

Đối với một ô được đóng hộp int, ví dụ: object boxedInt = 0 được xác định trong mã của bạn, cả hai số is objectis int trả lại false trong Cửa sổ ngay lập tức của Visual Studio. Đây là một lỗi, phải không?"là" toán tử hoạt động khác trong Cửa sổ ngay lập tức cho các giá trị được đóng gói

Code:

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 

Ngay lập tức cửa sổ:

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
Version 15.3.3
VisualStudio.15.Release/15.3.3 + 26.730,12

Microsoft .NET F ramework
Version 4.7.02046

Visual C# 2017 00369-60000-00001-AA135


Ảnh chụp màn hình với Watch cửa sổ:

+0

Yes. Ví dụ: khi đặt điểm ngắt sau mã trên. –

+0

Tôi nghĩ bạn phải làm- 'boxedInt == typeof (đối tượng);' –

+3

@SouvikGhosh 'boxedInt' không phải là một đối tượng' Loại', do đó sẽ không bao giờ đúng. – Servy

Trả lời

1

Dưới Tools-> Option-> Debugging , hãy bật tùy chọn "Sử dụng các bộ đánh giá biểu thức C# và VB cũ", gỡ lỗi lại.

enter image description here

Cập nhật:

Vấn đề này đã được báo cáo ở đây:

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

+0

Đây là một câu trả lời hữu ích vì nó hoạt động thực sự. Tuy nhiên, nó không trả lời lý do tại sao bạn sẽ muốn các hành vi quan sát trong chế độ không di sản (nghĩa là phải được tốt hơn) và cho dù đó có thể là một lỗi. Tôi bỏ phiếu này nhưng không chấp nhận nó như là câu trả lời đúng. –

+0

@Marco Eckstein, tôi sẽ báo cáo vấn đề này và thảo luận với các thành viên khác, nếu tôi nhận được bất kỳ thông tin mới nhất nào, tôi sẽ chia sẻ thông tin đó tại đây. –

+0

@Marco Eckstein, Sự cố tương tự đã được báo cáo tại đây: https://developercommunity.visualstudio.com/content/problem/31283/and-operation-of-boolean-is-wrong.html, các thành viên trong nhóm sản phẩm sẽ theo dõi nó , bạn cũng có thể chia sẻ nhận xét của mình ở đó. –

Các vấn đề liên quan