为什么HashSet的,但是不能设置在C#中?

为什么HashSet的,但是不能设置在C#中?

问题描述:

我的理解是,C#在某种意义上 HashSet的设置类型。我明白 HashSet的是。但是,为什么设置是一个单独的词吗?为什么不是每个集的HashSet<对象>

My understanding is that C# has in some sense HashSet and set types. I understand what HashSet is. But why set is a separate word? Why not every set is HashSet<Object>?

为什么C#有没有通用设置类型,类似于词典键入?从我的角度来看,我想有一组标准的查找/添加/删除的性能。我不太喜欢它是否与哈希或别的东西来实现。那么,为什么不作出这样实际上在这个版本的C#实现为 HashSet的但也许在未来的版本有所不同的一组类?

Why does C# has no generic Set type, similar to Dictionary type? From my point of view, I would like to have a set with standard lookup/addition/deletion performance. I wouldn't care much whether it is realized with hashes or something else. So why not make a set class that would actually be implemented as a HashSet in this version of C# but perhaps somewhat different in a future version?

或者为什么不至少接口的ISet

Or why not at least interface ISet?

据悉感谢大家谁如下回答:的ICollection 实现很多你想从的ISet 期待什么code>。从我的角度来看,虽然,的ICollection 工具的IEnumerable 同时集不必须是枚举的例子--- :1和2之间设定的实数(更,可以动态生成集)。我同意这是一个轻微的咆哮,如普通程序员很少需要不可数集。

Learned thanks to everyone who answered below: ICollection implements a lot of what you'd expect from ISet. From my point of view, though, ICollection implements IEnumerable while sets don't have to be enumerable --- example: set of real numbers between 1 and 2 (even more, sets can be generated dynamically). I agree this is a minor rant, as 'normal programmers' rarely need uncountable sets.

好吧,我想我明白了。 HashSet的是绝对意味着要叫设置但这个词设置在某种意义上保留。更具体地讲,.NET架构的创建者想拥有类为不同的语言一组一致的(原文如此)。这意味着,标准类的每个名称不得与.NET语言的任何关键字相符。这个词设置,但是,在VB.NET中使用,这实际上是不区分大小写(是吗?),所以遗憾的是没有余地也耍花招

Ok, I think I get it. HashSet was absolutely meant to be called Set but the word Set is reserved in some sense. More specifically, creators of .NET architecture wanted to have a consistent set (sic!) of classes for different languages. This means that every name of the standard class must not coincide with any keyword in the .NET languages. The word Set, however, is used in VB.NET which is actually case-insensitive (is it?) so unfortunately there is no room for maneuvre there.

解开了谜底:)

新的答案被亚历克斯Y.链接到它描述了即将到来的.NET的 MSDN页面 4.0界面的 的ISet 因为我认为它应该其行为相当多,由 HashedSet 实施。好结局。

The new answer by Alex Y. links to the MSDN page which describes the upcoming .NET 4.0 interface ISet which behaves pretty much as I thought it should and is implemented by HashedSet. Happy end.

(你原来的问题设置已经回答了。IIRC, 集是在英语中最不同的含义......一词显然这在计算也产生了影响。)

(Your original question about set has been answered. IIRC, "set" is the word with the most different meanings in the English language... obviously this has an impact in computing too.)

我认为它的罚款有的HashSet&LT; T&GT; 使用该名称,但我当然欢迎了的ISet&LT; T&GT; 接口。鉴于的HashSet&LT; T&GT; 只有到达.NET 3.5(这本身是令人惊讶)我怀疑我们最终可能会得到基于集合类型更完整的集合。特别是,相当于Java的 LinkedHashSet ,它维护插入顺序,会在某些情况下是有用的。

I think it's fine to have HashSet<T> with that name, but I'd certainly welcome an ISet<T> interface. Given that HashSet<T> only arrived in .NET 3.5 (which in itself was surprising) I suspect we may eventually get a more complete collection of set-based types. In particular, the equivalent of Java's LinkedHashSet, which maintains insertion order, would be useful in some cases.

要平心而论,的ICollection&LT; T&GT; 接口实际上涵盖了大部分的你想要什么的ISet&LT; T&GT; ,所以也许这不是必需的。不过,你可能会说一组的核心目的(这主要是关于遏制,只有切向大约能够遍历元素)是不太一样的集合。这是棘手。事实上,真正的数学集合可能不是迭代或可数 - 例如,你可以有实数集的1和2之间。如果你有一个任意精度的数字类型,数量将是无限的,遍历将没有任何意义。

To be fair, the ICollection<T> interface actually covers most of what you'd want in ISet<T>, so maybe that isn't required. However, you could argue that the core purpose of a set (which is mostly about containment, and only tangentially about being able to iterate over the elements) isn't quite the same as a collection. It's tricky. In fact, a truly mathematical set may not be iterable or countable - for instance, you could have "the set of real numbers between 1 and 2." If you had an arbitrary-precision numeric type, the count would be infinite and iterating over it wouldn't make any sense.

同样的加入到的想法集不总是有意义。可变性是一个棘手的业务集合命名时:(

Likewise the idea of "adding" to a set doesn't always make sense. Mutability is a tricky business when naming collections :(

编辑:好的,响应注释:关键字设置绝不是一个传统的做用Visual Basic,它是其中的​​设置的属性的其中的的价值,VS GET 检索操作的操作。这有什么好做了一组作为操作的想法。

Okay, responding to the comment: the keyword set is in no way a legacy to do with Visual Basic. It's the operation which sets the value of a property, vs get which retrieves the operation. This has nothing to do with the idea of a set as an operation.

想象一下,而不是关键字实际上分配,如:

Imagine that instead the keywords were actually fetch and assign, e.g.

// Not real code!
public int Foo
{
    fetch
    {
        return fooField;
    } 
    assign
    {
        fooField = value;
    } 
}

是目的明确在那里?现在的真正的在C#中的等价物就是

Is the purpose clear there? Now the real equivalent of that in C# is just

public int Foo
{
    get
    {
        return fooField;
    } 
    set
    {
        fooField = value;
    } 
}

所以,如果你写的:

x = y.Foo;

这将使用的的 GET 部分属性。如果你写的:

that will use the get part of the property. If you write:

y.Foo = x;

这将使用设置部分。

是更清楚?