为什么C#不执行索引属性? [英] Why C# doesn't implement indexed properties?

查看:123
本文介绍了为什么C#不执行索引属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我知道......埃里克利珀的回答这种问题通常是这样的,因为它不是设计的值得的成本,实现,测试和记录它的。

I know, I know... Eric Lippert's answer to this kind of question is usually something like "because it wasn't worth the cost of designing, implementing, testing and documenting it".

不过,我仍然想一个更好的解释......我在读的这个博客帖子大约新的C#4功能,并在有关COM互操作的部分,下面的部分引起了我的注意:

But still, I'd like a better explanation... I was reading this blog post about new C# 4 features, and in the section about COM Interop, the following part caught my attention :

顺便说一句,这code使用多了一个新功能:索引属性(于量程后的方括号中一探究竟。)但是此功能仅适用于COM互操作;你不能在C#中创建自己的索引属性4.0

By the way, this code uses one more new feature: indexed properties (take a closer look at those square brackets after Range.) But this feature is available only for COM interop; you cannot create your own indexed properties in C# 4.0.

OK,但为什么呢?我已经知道,并感到遗憾的是这是不可能建立在C#中索引的属性,但是这句话让我再想想。我可以看到几个很好的理由来实现它:

OK, but why ? I already knew and regretted that it wasn't possible to create indexed properties in C#, but this sentence made me think again about it. I can see several good reasons to implement it :


  • 的CLR支持的话(例如, PropertyInfo.GetValue 有一个首页参数),所以这是一个遗憾我们不能利用它在C#

  • 这是支持COM互操作,如在文章中(使用动态调度)

  • 它在VB.NET实施

  • 它已经可以创建索引,也就是申请一个索引对象本身,因此它很可能是没有什么大不了的,延长的想法性能,保持相同的语法,只是替换与属性名称

  • the CLR supports it (for instance, PropertyInfo.GetValue has an index parameter), so it's a pity we can't take advantage of it in C#
  • it is supported for COM interop, as shown in the article (using dynamic dispatch)
  • it is implemented in VB.NET
  • it is already possible to create indexers, i.e. to apply an index to the object itself, so it would probably be no big deal to extend the idea to properties, keeping the same syntax and just replacing this with a property name

这将使写那种东西:

public class Foo
{
    private string[] _values = new string[3];
    public string Values[int index]
    {
        get { return _values[index]; }
        set { _values[index] = value; }
    }
}

目前我所知道的唯一的解决方法是创建一个内部类( Values​​Collection 例如)实现索引器,并更改属性,使其返回内部类的一个实例。

Currently the only workaround that I know is to create an inner class (ValuesCollection for instance) that implements an indexer, and change the Values property so that it returns an instance of that inner class.

这是很容易做到的,但恼人的...因此,或许可以编译为我们做到这一点!一个办法是,生成实现索引的内部类,并通过一个公共通用接口将其暴露

This is very easy to do, but annoying... So perhaps the compiler could do it for us ! An option would be to generate an inner class that implements the indexer, and expose it through a public generic interface :

// interface defined in the namespace System
public interface IIndexer<TIndex, TValue>
{
    TValue this[TIndex index]  { get; set; }
}

public class Foo
{
    private string[] _values = new string[3];

    private class <>c__DisplayClass1 : IIndexer<int, string>
    {
        private Foo _foo;
        public <>c__DisplayClass1(Foo foo)
        {
            _foo = foo;
        }

        public string this[int index]
        {
            get { return _foo._values[index]; }
            set { _foo._values[index] = value; }
        }
    }

    private IIndexer<int, string> <>f__valuesIndexer;
    public IIndexer<int, string> Values
    {
        get
        {
            if (<>f__valuesIndexer == null)
                <>f__valuesIndexer = new <>c__DisplayClass1(this);
            return <>f__valuesIndexer;
        }
    }
}

不过,当然,在这种情况下,物业会的真正的返回 IIndexer&LT; INT,串&GT; ,并不会真的是索引属性...这将是更好地产生一个真正的CLR索引属性。

But of course, in that case the property would actually return a IIndexer<int, string>, and wouldn't really be an indexed property... It would be better to generate a real CLR indexed property.

你觉得呢?你想看到在C#中这个功能吗?如果不是,为什么?

What do you think ? Would you like to see this feature in C# ? If not, why ?

推荐答案

下面是我们如何设计C#4。

Here's how we designed C# 4.

首先,我们尽了一切可能的功能,我们可以认为增加了语言的列表。

First we made a list of every possible feature we could think of adding to the language.

然后我们分时段的特点为这是不好的,我们决不能这样做,这是真棒,我们必须这样做,而这是好事,但我们不做这个时间。

Then we bucketed the features into "this is bad, we must never do it", "this is awesome, we have to do it", and "this is good but let's not do it this time".

然后,我们看我们有多少预算来设计,实现,测试,文档,船舶和维护必须拥有的功能和发现,我们超过了预算分别为100%。

Then we looked at how much budget we had to design, implement, test, document, ship and maintain the "gotta have" features and discovered that we were 100% over budget.

所以我们感动了一堆东西从必须拥有斗到最好有水桶。

So we moved a bunch of stuff from the "gotta have" bucket to the "nice to have" bucket.

索引属性从来没有在任何地方的附近的在顶部的必须拥有名单。他们是非常低的好名单上,有糟糕的主意名单调情。

Indexed properties were never anywhere near the top of the "gotta have" list. They are very low on the "nice" list and flirting with the "bad idea" list.

我们花设计,实施,测试,记录或保持很好的特征量x每一分钟是我们不能真棒特征的花一分钟,B,C,D,E,F和G我们必须无情地优先考虑这样我们只做最好的功能。索引的属性将是很好,但好是不是在任何地方甚至接近不够好,真正得到落实。

Every minute we spend designing, implementing, testing, documenting or maintaining nice feature X is a minute we can't spend on awesome features A, B, C, D, E, F and G. We have to ruthlessly prioritize so that we only do the best possible features. Indexed properties would be nice, but nice isn't anywhere even close to good enough to actually get implemented.

这篇关于为什么C#不执行索引属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆