I’m new here and somewhat inexperienced with C#. I’ve been searching through the MSDN documentation and Google, but can’t find the answer to this (I try to word it as generally as possible):
I want to store a fixed-length ordered sequence of integers in a list or array, and then make an array of these integer arrays. Does anyone know how I can count the number of distinct integer arrays, and what specific data type(s) I should use (List, plain array, etc.)? I don’t have the exact code I’ve been working with, but here’s something similar to what I have been trying:
int[] set1 = {2, 56, 8};
int[] set2 = {8, 25, 90};
int[] set3 = {2, 56, 8};
var superset = new List<int[]>;
superset.Add(set1);
superset.Add(set2);
superset.Add(set3);
Console.Out.WriteLine(superset.Distinct().Count()); // would like this to output 2, but Distinct() doesn't seem to actually work and I would get 3
Keep in mind a famous quote:
It sounds like the goal here is to be able to use simple and expressive code (
.Distinct()) to compare your data. In that case, I’d recommend upgrading from simple arrays to richer objects. Something like this:Then you can have an array of these objects, instead of an array of arrays. The benefit here is that you can endow this object with richer functionality. Such as:
Now your smarter data type can be used more effectively by dumber code. (Just referencing the quote, don’t think of it as saying that you’re writing dumb code or anything unhelpful like that.) This is generally preferred because it means that you don’t have to re-write the comparison logic in multiple places if it needs to be used in multiple places. The comparison logic happens internal to the data type, not in procedural code.
Note that this is untested and freehand code. If I missed something in the implementation, please correct me 🙂