4 ms·
I don't understand the point you're trying to make with your example. Is the concern here that in C# `FileInfo` does not override `Equals` and because this is i
by macspoofing 4y ago
I don't understand the point you're trying to make with your example. Is the concern here that in C# `FileInfo` does not override `Equals` and because this is inconsistent with other standard lib objects this is a trap for developers? If that's the case, well, Java has its own share of weird and inconsistent behaviour that developers simply need to be aware of.
---
On a tangent, is there a reason why FileInfo does not override `Equals`?
- thangalin 4y agohttps://en.wikipedia.org/wiki/Principle_of_least_astonishment#Formulation https://en.wikipedia.org/wiki/Principle_of_least_astonishmen... Here's another: string path = "/tmp/filename.txt"; FileInfo f = new FileInfo( path ); File.CreateText( path ).Dispose(); Assert.True( f.Exists ); f.Delete(); Assert.False( f.Exists ); Astonishingly, the test fails because the result of Exists is cached. ¯\_(ツ)_/¯ My expectation is that if I put two things that are identical---for all intents and purposes---into a set, only one copy is added to the set. Likewise, upon deleting a file, I expect that testing for its existence always returns false. Both of these expectations are based on my mental models for math and file systems. I haven't tested the behaviour of caching file states in Java, but I suspect it will return false if the file is deleted. Point being, in my limited time with C#, I've found it to violate the principle of least astonishment in ways that Java does not.
- jcparkyn 4y agoFileInfo is a stateful reference type, so overriding Equals and GetHashCode to use value equality would be a Bad Idea™. E.g.: var f = new FileInfo("image.jpg"); var set = new HashSet<FileInfo> { f }; f.MoveTo("image2.jpg"); Console.WriteLine(set.Contains(f)); // Would be false (sometimes)