Collections in C #

I am coming from the background in C ++ and I am trying to teach myself C #. I am still a student, and this is not a school project, and I am now in a break. The focus was on C / C ++, java

I have an old school project from two years ago, which I did in C ++, which stores a DVD, a book, a DVD, information in a container, and then shows it. I am trying to make the same program in C #

I have two questions that are very similar.

My program uses typedef on the container and made the container objects as follows:

typedef set<Item*> ItemSet; ItemSet allBooks; // holds all the information ItemSet allCDS; ItemSet allDVDs; 

First question: is there a typdef in C #, I think its equivalent uses? But I can not use it on the container. Is there a way to do something similar above in C # or just create multiple sets? I decided that the Hashset is like a set in C ++

 HashSet<Item> keys = new HashSet<Item>(); 

Second question: I also used typedef for dialing outside the class, and then in the private section I made a dialing object. Can I do this in C #?

 typedef set<string> StringSet; class Item { private: string Title; StringSet* Keys; // holds keywords 

I understand that C # has no pointers!

+4
source share
4 answers

C # has nothing like typedef . You just use type names directly.

 HashSet<Item> allBooks = new HashSet<Item>(); HashSet<Item> allCDs= new HashSet<Item>(); HashSet<Item> allDVDs = new HashSet<Item>(); HashSet<string> keys = new HashSet<string>(); 

So...

 class Item { private string title; private HashSet<string> keys = new HashSet<string>(); // holds keywords 

The using statement declares the namespaces that the compiler must look for to resolve type names. So, to work above, you need

 using System.Collections.Generic; 

at the top of the source file.

Without the using statement, you will need to do

 System.Collections.Generic.HashSet<Item> allBooks = new System.Collections.Generic.HashSet<Item>(); 
+3
source

In C #, there is no concept that compares with a C ++ typedef . To simplify your life and save on some printing presses, C # allows you to skip the type of declaration / initialization of local variables:

 var keys = new HashSet<Item>(); 

coincides with

 HashSet<Item> keys = new HashSet<Item>(); 

You still need to use the full type in member ads. However, it is recommended that you declare elements as interfaces, and not as exact types:

 class MyClass { ISet<Item> allBooks = new HashSet<Item>(); } 

This may allow you to switch the implementation later without changing anything in your code.

+2
source

C # obviously does not have a typedef , the closest thing is the using declaration to create a type alias. At the top of your C # file, you can do something like:

 using StringSet = System.Collections.Generic.HashSet<string>; 

Then later you can do something like:

 var set = new StringSet(); 
+1
source

There is a typedef equivalent to using directives , but I have not seen it widely used. Common types are commonly used. If you have a need (for example, you need to save other data along with a collection of elements), create a class like public class ItemSet : List<Item> .

HashSet<T> should be used when the set should not contain the same element two or more times, and the Object.Equals and Object.GetHashCode make sense for your data type (by default, they essentially identify the instance, not the value of the object ) It will automatically use them only to add an object if it does not already exist in the set. A much more general approach is to use List<T> to store items. From the fact that I know little about your situation, List<T> should work.

Therefore, I would recommend something like:

 class Item { private string Title; List<string> Keys; // holds keywords } 

and

 List<Item> allBooks = new List<Item>(); List<Item> allCDs= new List<Item>(); List<Item> allDVDs = new List<Item>(); 
+1
source

Source: https://habr.com/ru/post/1387307/


All Articles