English 中文(简体)
界面和索引器
原标题:Interfaces and Indexers

我有一个界面 被这样使用:

var descr = new IMyInterface[3];

乍一看,我以为这没有任何意义,因为它看起来像一个界面正在被即时转换(这是不可能的 ) 。 因此我认为这可能是对界面中定义的索引器的呼唤,但我还是不太明白它是如何运作的。所以我的问题是:

  1. What is this call doing? Is it calling an indexer defined in the interface?
  2. What implementation of the interface s indexer is being used?

感谢您的帮助:)

最佳回答

您正在创建一个由 3 IMy Interface 组成的数组... 但每个项在数组中都是空的 。

问题回答

它创建了一个包含三个元素的 IMy Interface 的阵列,即 IMy Interface[] 。这只是创建数组时常用的 new T[n] 语法,与 new int[3] 没有区别。

从概念上来说,它类似于 new Array<IMy Interface>(3) ,但由于历史原因,数组不是通用类型。

数组是一个混凝土类, 所以您可以创建一个实例, 即使成员类型是一个接口。 成员将被初始化为 < code> default( T) , 即 < code> null

这与索引员无关。


可以在界面上定义索引者, 就像您在类中一样。 但使用它们的语法并不包含 < code> new

在某些COM相关假设情景中,还有可能即时化接口。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签