Đố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 object
và is 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ổ:
Yes. Ví dụ: khi đặt điểm ngắt sau mã trên. –
Tôi nghĩ bạn phải làm- 'boxedInt == typeof (đối tượng);' –
@SouvikGhosh 'boxedInt' không phải là một đối tượng' Loại', do đó sẽ không bao giờ đúng. – Servy