- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我最近对 List<>
做了一些粗略的性能测量对比[]
对于一系列小型结构。 System.Array 似乎赢得了轻而易举的胜利,所以我选择了它。
我才刚刚意识到 System.Array 包含对象类型,所以用结构填充它肯定会导致装箱吗?
然而,the MSDN entry for System.Array状态:
In the .NET Framework version 2.0, the Array class implements the
System.Collections.Generic.IList<T>
,System.Collections.Generic.ICollection<T>
, andSystem.Collections.Generic.IEnumerable<T>
generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations).
这是否意味着拳击终究不会发生? (并会解释我的表现结果)
最佳答案
如果使用索引器表示法,则使用数组不会装箱。例如
new int[2];
x=[1]=3;
编译为以下 IL(注意行号是不相关的,因为它们来自其他代码片段)
IL_0011: ldc.i4.2
IL_0012: newarr System.Int32
IL_0017: stfld Int32[] x
IL_001c: ldarg.0
IL_001d: ldfld Int32[] x
IL_0022: ldc.i4.1
IL_0023: ldc.i4.3
IL_0024: stelem.i4
对于不能使用索引器的语言(我真的不知道它们是否存在)在编译时为数组创建了 2 个其他方法。
它创建这些公共(public)方法::
public int Get(int index)
public void Set(int index,int value)
这些方法也不会装箱,通常不能通过 C# 访问。 (不要问我为什么它们是公共(public)方法)。您可以使用 IL 或通过为它们创建委托(delegate)来执行它们。它们速度较慢,因为您被迫执行 callvirt 来调用这些方法。
stelem.* 和 ldelem.* 系列用于处理强类型数组类型的存储。使用泛型时,通常会附加以下前缀 constrained
或 readonly
使用 T[]
时. stelem.*
type 通常不检查类型。例如。使用 stelem.i4
比使用 stelem.any Int32
更快除非你用 readonly
作为前缀因为否则它会强制进行类型检查。
现在类型检查对值类型数组完全没用,因为它们不是协变的!
因为运行时生成从零开始的一维数组(称为 SZ_array 或向量类型)类型,所以它们是 native 已知的。
它们有一系列 il 操作码:newarr
, stelem.*
, ldelem.*
, ldlen
等等
List<T>
类型使用 T[]
用于其在 BCL 的 Microsoft 实现中的后备存储。 List<T>
不框。无论使用列表还是数组,您都将内容存储在数组中。
关于c# - System.Array 是否对值类型执行装箱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8213887/
我有一个带有帮助页面的 Web API 2 项目,该项目在本地运行良好,但当我将其推送到 Azure 时抛出此错误: Method not found: 'System.String System.S
我有两台服务器,但通常运行相同的设置 - IIS、SQL Server 等。一台给我这个错误,另一台没有。我从 Visual Studio 向两者发布相同的代码。 它们都在运行 .NET CLR Ve
System.out声明为 public static final PrintStream out。 但是你可以调用System.setOut()重新分配它。 嗯?如果它是 final,这怎么可能?
System.out被声明为 public static final PrintStream out。 但是您可以调用System.setOut()重新分配它。 嗯?如果是 final,这怎么可能?
我有这个 linq 查询: private void GetReceivedInvoiceTasks(User user, List tasks) { var areaIds = user.A
我有一个 MonoTouch 应用程序,当我为设备编译它时,出现以下错误: Error MT2002: Can not resolve reference: System.Boolean System
您好,我有一个名为 DailyVisitReport 的 View 。在该 View 中,我有两个名为 FromDate 和 toDate 的字段。如果我选择 FromDate 和 ToDate 取决
是否可以从 ObjectContext 对象中读取元组列表? 我在存储过程中有类似这样的数据库查询 SELECT T.Id as Item1, -- this is guid T.Wo
我正在尝试创建 Odata 端点,但每当我尝试执行任何涉及日期的查询时都会收到此错误。 我在下面的非常简单示例中重新创建了它。 数据库表 EDMX(片段)
我正在尝试创建 Odata 端点,但每当我尝试执行任何涉及日期的查询时都会收到此错误。 我在下面的非常简单示例中重新创建了它。 数据库表 EDMX(片段)
我有一个方法可以从数据读取器的数据中生成类类型列表。 if (datareader != null && datareader .HasRows) { Dictionary pDict= GetP
我有一些旧的 C++ 代码,它们使用 stdio 进行输入和输出。该代码还通过 fork 生成新进程。它将 stdio 重新映射到每个新进程,以便每个 session 获取其各自的数据。 我正在考虑使
我的应用程序可以很好地构建/链接/部署到模拟器,但我只是第一次尝试将应用程序构建/部署到真实设备,并且链接器失败。 我不使用 System.Console或 ConsoleColor在我的应用程序的任
主要是我很好奇。 我们有一个名为 Unit 的对象在我们的代码库中 - 代表桥梁或道路的组件。在我们的例子中,看到带有 Unit 的 ReactiveUI 命令可能会模棱两可。作为声明中的泛型之一。
我试图将Object变量转换为StreamWriter。但是,它不起作用。有什么错? StreamWriter file = (StreamWriter) myObject; 最佳答案 myObjec
为什么以下不编译? using System; using System.Linq; using System.Linq.Expressions; public static class Extens
我正在使用 Visual Studio Community 2015 开发面向 .NET 4.5 的 Visual Basic 应用程序.我没有编写应用程序,所以我使用 NuGet 添加了所有缺失的依
我刚刚开始使用 powershell,我正在制作一个非常简单的加密功能。我想获取字符串中的每个字符,将其转换为 int 并添加一个选定的数字,然后将其转换回一个字符。 这工作正常: function
一些使用我的应用程序的人似乎变得越来越 System.MissingMethodException: Method not found: 'System.Object System.Windows.T
我是 C# 和实体的新手 我想知道是否有人在这里帮助我。我选择了哪个返回我的 customerid,所以我想将它作为参数传递给我的构造函数,我的构造函数参数类型是 guid 但我的选择类型不同,我不知
我是一名优秀的程序员,十分优秀!