从方法中返回词典(Returning dictionaries from methods)

编程入门 行业动态 更新时间:2024-10-17 19:31:46
从方法中返回词典(Returning dictionaries from methods)

这些类型的方法一直困扰着我。 在不深入研究代码的情况下,我不知道该字典中的键或子值应该是什么。 我已经做了大约一千次,我敢肯定。 但它一直困扰着我,因为它缺乏如此多的信息。

IDictionary<int,HashSet<int>> GetExportedCharges() { ... } void GetVisitStatuses(IDictionary<int,HashSet<int>> exportedCharges) { ... }

将材料放在字典中是有意义的,但所有IDictionary方法都有非常抽象的参数。 我想我可以创建一个新类,实现IDictionary,并重命名所有参数。 这看起来有点矫枉过正。 希望c#有一个'typdef'指令。

你怎么避免像这样回复词典? 或者你完全避免这种情况?

These types of methods have always bothered me. Without digging through the code, I have no idea what the key or sub-value is supposed to be in this dictionary. I've done this myself about a thousand times, I'm sure. But it has always bothered me because it lacks so much information.

IDictionary<int,HashSet<int>> GetExportedCharges() { ... } void GetVisitStatuses(IDictionary<int,HashSet<int>> exportedCharges) { ... }

Having the material in a dictionary makes sense, but all of the IDictionary methods have very abstract parameters. I suppose I could create a new class, implement IDictionary, and rename all of the parameters. It just seems like overkill. Makes be wish c# had a 'typdef' directive.

How do you avoid returning dictionaries like this? Or do you avoid this at all?

最满意答案

您可能会发现一个非常简单的解决方案:

public class IdToSetOfWidgetNumbersMap : Dictionary<int,HashSet<int>> { }

优点:

看到返回IdToSetOfWidgetNumbersMap的方法并没有留下很多问题 该类可以完全为空,或者如果您愿意,可以选择为Dictionary公开的成员提供别名

坏处:

您需要为每种类型的字典创建一个do-nothing类

There is a very simple solution that you might find adequate:

public class IdToSetOfWidgetNumbersMap : Dictionary<int,HashSet<int>> { }

Advantages:

Seeing a method returning an IdToSetOfWidgetNumbersMap doesn't leave many questions open The class can be left totally empty, or you can choose to provide aliases for the members exposed by Dictionary if you prefer

Disadvantage:

You need to create a do-nothing class for each type of dictionary

更多推荐

本文发布于:2023-07-26 00:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词典   方法   Returning   dictionaries   methods

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!