在 C# .NET 中,是否有 StringDictionary 没有复制构造函数的原因?

发布于 2024-07-26 15:22:15 字数 557 浏览 3 评论 0 原文

如果这是一个愚蠢的问题,我深表歉意,但请听我说:

Dictionary<string, string> genericDict = new Dictionary<string, string>;
genericDict.Add("blah", "bloop");
// Use the copy constructor to create a copy of this dictionary
return new Dictionary<string, string>(genericDict);

在上面的代码示例中,我可以创建通用字典的副本。

现在假设我正在使用 System.Collections.Specialized.StringDictionary,因为我不想在任何地方键入“字符串”类型。 StringDictionary 没有复制构造函数! 事实上,它只有默认的构造函数。

当然,我可以迭代 StringDictionary 并手动添加每个键/值对,但我不想:-P

为什么没有复制构造函数? 我在这里错过了什么吗?

I apologize if this is a dumb question, but hear me out:

Dictionary<string, string> genericDict = new Dictionary<string, string>;
genericDict.Add("blah", "bloop");
// Use the copy constructor to create a copy of this dictionary
return new Dictionary<string, string>(genericDict);

In the above code sample, I can create a copy of a generic dictionary.

Now suppose I'm using a System.Collections.Specialized.StringDictionary, because I don't feel like typing the "string" types everywhere. StringDictionary has no copy constructor! In fact, it only has the default constructor.

Sure, I can iterate through the StringDictionary and add each key/value pair manually, but I don't want to :-P

Why no copy constructor? Am I missing something here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴者 2024-08-02 15:22:15

StringDictionary 类型已经过时了。 我认为 Dictionary 就是您想要在此处使用的内容。

Dictionary 类型实现了一些强类型接口(ICollection>IEnumerable ;>),这使得它比 StringDictionary 类型更有用。

虽然 StringDictionary 类型是强类型的,但我不会仅仅因为懒惰而建议使用它。

The StringDictionary type is rather obsolete. I think that Dictionary<String,String> is what you want to use here.

The Dictionary<TKey,TValue> type implements some strongly-typed interfaces (ICollection<KeyValuePair<TKey, TValue>> and IEnumerable<KeyValuePair<TKey, TValue>>) which makes it more useful than the StringDictionary type.

While the StringDictionary type is strongly typed I wouldn't advise its use for the sake of laziness alone.

眼趣 2024-08-02 15:22:15

如果您确实想使用 StringDictionary(也许是为了支持旧版应用程序),您可以创建一个扩展方法:

public static StringDictionary NewCopy(this StringDictionary olddict)
{
    var newdict = new StringDictionary();
    foreach (string key in olddict.Keys)
    {
        newdict.Add(key, olddict[key]);
    }
    return newdict;
}

然后您可以执行以下操作:

StringDictionary newdict = olddict.NewCopy();

If you really want to use a StringDictionary (perhaps to support a legacy application), you can create an extension method:

public static StringDictionary NewCopy(this StringDictionary olddict)
{
    var newdict = new StringDictionary();
    foreach (string key in olddict.Keys)
    {
        newdict.Add(key, olddict[key]);
    }
    return newdict;
}

Then you can do this:

StringDictionary newdict = olddict.NewCopy();
追我者格杀勿论 2024-08-02 15:22:15

我同意共识 - 你最好使用 Dictionary。 但如果您不喜欢一直输入泛型类型,您可以创建自己的子类:

public class StringDict : Dictionary<string, string>
{
  // duplicate all Dictionary's constructors here, calling the base constructor for each. 
}

Et Voila! 每当您想使用字符串 Dictionary 时,您可以使用StringDict 相反。

I agree with the consensus -- you're better off using Dictionary<string, string>. But if you don't like typing the generic types all the time, you could create your own subclass:

public class StringDict : Dictionary<string, string>
{
  // duplicate all Dictionary's constructors here, calling the base constructor for each. 
}

Et Voila! Whenever you want to use a string Dictionary, you use StringDict instead.

活雷疯 2024-08-02 15:22:15

如果您不想在任何地方键入“字符串”类型,但又对 Dictionary 感到满意,则创建一个新类,该类是 Dictionary< /code> 并且不要覆盖任何方法。 您甚至可以将其称为 StringDictionary,只要它位于不同的命名空间中即可。

If you don't want to type the "string" types anywhere but are otherwise happy with Dictionary<string,string>, then create a new class that subclasses Dictionary<string,string> and just don't override any methods. You can even call it StringDictionary, as long as it's in a different namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文