我来自 C++ 背景,我正在尝试自学 C# 我仍然是一名学生,这不是一个学校项目,我现在正在休息。我的主要关注点是 C/C++、java
我有一个 2 年前用 C++ 完成的旧学校项目,将 DVD、书籍、DVD、信息存储在一个容器中,然后显示它。我正在尝试用 C# 编写相同的程序
我有两个非常相似的问题..
在我的程序中,在容器上使用了 typedef,并像这样创建了容器的对象:
typedef set<Item*> ItemSet;
ItemSet allBooks; // holds all the information
ItemSet allCDS;
ItemSet allDVDs;
第一个问题是 C# 中没有 typdef 我认为它的等效项正在使用?但我不能在容器上使用它。有没有办法在 C# 中做类似上面的事情,或者我应该创建多个集合? 我认为 Hashset 类似于 C++ 中的集合
HashSet<Item> keys = new HashSet<Item>();
第二个问题是我还在课外的集合上使用了 typedef,然后在私有(private)部分中使用了 typedef 我做了和对象的集合。我可以在 C# 中执行此操作吗?
typedef set<string> StringSet;
class Item
{
private:
string Title;
StringSet* Keys; // holds keywords
我确实意识到 C# 没有指针!
最佳答案
C# 没有类似typedef
的东西。您只需直接使用类型名称即可。
HashSet<Item> allBooks = new HashSet<Item>();
HashSet<Item> allCDs= new HashSet<Item>();
HashSet<Item> allDVDs = new HashSet<Item>();
HashSet<string> keys = new HashSet<string>();
所以...
class Item
{
private string title;
private HashSet<string> keys = new HashSet<string>(); // holds keywords
using
语句声明编译器应搜索以解析类型名称的命名空间。所以为了上面的工作你需要
using System.Collections.Generic;
在源文件的顶部。
如果没有 using
语句,你必须这样做
System.Collections.Generic.HashSet<Item> allBooks = new System.Collections.Generic.HashSet<Item>();
关于c# - C# 中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8584675/