Cause: A call is made to a method that returns a System.Object instance and the instance is cast to a value type.
Rule Description
Casting an Object instance to a value type invokes an unboxing operation. In an unboxing operation, the runtime first checks whether the instance is null and then determines whether the instance represents the specified value type. If so, the runtime returns a reference to the data portion of the instance and copies the fields from the heap to the stack. These operations degrade performance.
How to Fix Violations
To fix a violation of this rule, replace the call with an equivalent strongly typed method. Use generics if available.
When to Exclude Messages
Exclude a message from this rule if an equivalent strongly typed method is not available. It is also safe to exclude a message from this rule, or ignore the rule entirely, if performance is not a concern.
Example Code
The following example shows a method, WeaklyTyped, which violates the rule and a method, StronglyTyped, which satisfies the rule.
[C#]
using System; namespace PerformanceLibrary { publicinterface IWork { object DoWork(); } publicclass Work : IWork { object IWork.DoWork() { return3; } publicint DoWork() { return3; } } publicclass NeedsWork { publicvoid WeaklyTyped() { IWork iwork =new Work(); // The following call violates the rule. int x = (int)iwork.DoWork(); } publicvoid StronglyTyped() { Work work =new Work(); int x = work.DoWork(); } } }
The following example shows a method, WeaklyTyped, which violates the rule and a method, UsesGenerics, which satisfies the rule using generics.
[C#]
using System; using System.Collections; using System.Collections.Generic; namespace PerformanceLibrary { publicclass AvoidBoxing { publicvoid WeaklyTyped() { ArrayList weaklyTypedList =new ArrayList(); weaklyTypedList.Add(3); weaklyTypedList.Add(5); weaklyTypedList.Add(11); int sum =0; for(int i =0; i < weaklyTypedList.Count; i++) { // The following call violates the rule. sum += (int)weaklyTypedList[i]; } } publicvoid UsesGenerics() { List<int> stronglyTypedList =new List<int>(); stronglyTypedList.Add(3); stronglyTypedList.Add(5); stronglyTypedList.Add(11); int sum =0; for(int i =0; i < stronglyTypedList.Count; i++) { sum += stronglyTypedList[i]; } } } }
[Visual Basic]
Imports System Imports System.Collections Imports System.Collections.Generic Namespace PerformanceLibraryNamespace PerformanceLibrary PublicClass AvoidBoxingClass AvoidBoxing Sub WeaklyTyped()Sub WeaklyTyped() Dim weaklyTypedList AsNew ArrayList() weaklyTypedList.Add(3) weaklyTypedList.Add(5) weaklyTypedList.Add(11) Dim sum AsInteger=0 For I AsInteger=0To weaklyTypedList.Count -1 ' The following call violates the rule. sum = sum +DirectCast(weaklyTypedList(I), Integer) Next End Sub Sub UsesGenerics()Sub UsesGenerics() Dim stronglyTypedList AsNew List(Of Integer) stronglyTypedList.Add(3) stronglyTypedList.Add(5) stronglyTypedList.Add(11) Dim sum AsInteger=0 For I AsInteger=0To stronglyTypedList.Count -1 sum = sum +DirectCast(stronglyTypedList(I), Integer) Next End Sub End Class End Namespace
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix