Is it safe to use a DirectoryInfo object as a key in a dictionary?

I use a dictionary using objects such as DirectoryInfo and FileInfo as keys. Is it safe to use them this way? How can I make sure that the type of an object can be safely used in this way?

+4
source share
3 answers

In general, the only way to know exactly how this will behave is to check the reference source. You can check the documentation to see if it is being implemented IEquatable<T>(in this case you will also get the โ€œexpectedโ€ behavior).

Equals GetHashCode, . , , , System.Object.

DirectoryInfo , , , , , , . FullName , aa string .

+7

, , DirectoryInfo, , -.

, !

new System.IO.DirectoryInfo(@"C:\").GetHashCode(); // 25422474
new System.IO.DirectoryInfo(@"C:\").GetHashCode(); // 48007696

- . , . .

, GetHashCode .

+3

, , "" . . , , , . , , .

+1

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


All Articles