C# - List of Lists - c#

I am coding in C# and I have a class with a property of type List<List<T>> that gets initialized in the constructor. The code is as follows:
public class MyMatrix<T>
{
private readonly List<List<T>> matrix;
public MyMatrix(int dimension)
{
this.matrix = new List<List<T>>();
for (int i = 0; i < dimension; i++)
{
List<T> subList = new List<T>();
this.matrix.Add(subList);
}
}
.....
The problem is that if I create a new object of type MyMatrix the sublists are empty so if I invoke the ToString() method of the class or any other method that returns the values contained in the sublists I get an OutOfOrder Exception as expected.
Get and Set methods are as follows:
public T Get(int row, int column)
{
return this.matrix[row][column];
}
public void Set(int row, int column, T value)
{
this.matrix[row].Insert(column, value);
}
If I initialize the sublists with a Set method then everything is fine obviously.
I can't change the constructor as it is up to the user of the class to initialize the sublists so it is not possible to know in advance what they are going to contain.
How would you manage the exceptions in the class methods or would you bother at all?

There are several approaches on managing exceptions in your case, and it depends on how you want to use the matrix class:
If you expect users to set values without initializing the row/column, then on the Set method I would just resize the list if necesary to accomodate the row/column arguments. You can always insert empty items in the list by using default(T) (this works both with value and reference objects). In this scenario, the Get method should check if the matrix coordinates exist and otherwise return default(T) so that no exceptions occurr.
If you expect users to always initialize the matrix, then just leave it as it is and throw exceptions. This is a clear hint that the application is misbehaving and the programmer must take care of this.
If you are trying to implement something like a Sparse Matrix, then using List<T> is not the best way and you should try another approach - for example using Dictionary<int, Dictionary<int, T>> or some sort of linked list. Anyway in this scenario, if you go for the Dictionary approach, you still need to take the same decisions as above (ie. throw if accessing a non existent coordinate or just return default(T))

What about C# 6.0?
public T Get(int row, int column)
{
return this.matrix[row]?[column] ?? default(T);
}

Related

Contains method using Linq/Lambda

This method finds different dates and adds to dates array. Is it possible to make this method with linq/lambda?
public static void FindDates(DateTime[] dates, LinkedList<Letter> L, out int counter)
{
counter = 0;
foreach (var let in L)
{
if (!dates.Contains(let.Date))
{
dates[counter] = let.Date;
counter++;
}
}
}
You need to change the prototype of the method, but you can do something like:
public static IReadOnlyList<DateTime> FindDates(IEnumerable<Letter> L)
{
return L.Select(l => l.Date).Distinct().ToList();
}
The value of counter can be retrieved easily by reading the Count property of the result list.
Overall, it's a good practice to avoid side-effects in methods as much as possible. Modifying an array passed as a parameter like you do is a good way to get bitten later.
Also, since the Linq extension methods are defined on IEnumerable<T>, we can change the parameter of the method to IEnumerable<Letter>. It'll work exactly the same with your LinkedList<Letter>, with the added benefit that it won't break if later you decide to use another collection type (such as List<Letter>)

Sorting a list with two parameters using CompareTo

I am presently sorting a C# list using the 'CompareTo' method in the object type contained in the list. I want to sort ascendingly all items by their WBS (Work Breakdown Structure) and I can manage this very well using the following code:
public int CompareTo(DisplayItemsEntity other)
{
string[] instanceWbsArray = this.WBS.Split('.');
string[] otherWbsArray = other.WBS.Split('.');
int result = 0;
for (int i = 0; i < maxLenght; i++)
{
if (instanceWbsArray[i].Equals(otherWbsArray[i]))
{
continue;
}
else
{
result = Int32.Parse(instanceWbsArray[i]).CompareTo(Int32.Parse(otherWbsArray[i]));
break;
}
}
return result;
}
Now, I would like to be able to sort considering more than one parameter, as in the project name alphabetically, before considering the second which would be the WBS. How can I do this?
I don't know the details of your class, so I'll provide an example using a list of strings and LINQ. OrderBy will order the strings alphabetically, ThenBy will order them by their lengths afterwards. You can adapt this sample to your needs easily enough.
var list = new List<string>
{
"Foo",
"Bar",
"Foobar"
};
var sortedList = list.OrderBy(i => i).
ThenBy(i => i.Length).
ToList();
What we generally do in cases like yours is this:
public int CompareTo( SomeClass other )
{
int result = this.SomeProperty.CompareTo( other.SomeProperty );
if( result != 0 )
return result;
result = this.AnotherProperty.CompareTo( other.AnotherProperty );
if( result != 0 )
return result;
[...]
return result;
}
P.S.
When posting code, please try to include only the code which is pertinent to your question. There is a load of stuff in the code that you posted that I did not need to read, and that in fact made my eyes hurt.
I like Eve's answer because of it's flexibility but I'm kinda surprised that no-one has mentioned creating a custom IComparer<T> instance
IComparer<T> is a generic interface that defines a method for comparing two instances of the type T. The advantage of using IComparer<T> is that you can create implementations for each sort order you commonly use and then use these as and when necessary. This allows you to create a default sort order in the types CompareTo() method and define alternative orders separately.
E.g.
public class MyComparer
: IComparer<YourType>
{
public int Compare(YourType x, YourType y)
{
//Add your comparison logic here
}
}
IComparer<T> is particularly useful for composition where you can do things like have a comparer which compares some properties of a given type using another comparer that operates on the type of the property.
It's also very useful if you ever need to define a sorting on a type you don't control. Another advantage it has is it doesn't require LINQ so can be used in older code (.Net 2.0 onwards)
First compare the project name alphabetically and if they are not equal return value, if not perform comparison based on second value
public int CompareTo(DisplayItemsEntity other)
{
if(other.ProjectName.CompareTo(this.ProjectName) != 0)
{
return other.ProjectName.CompareTo(this.ProjectName)
}
//else do the second comparison and return
return result;
}

How might a List<T> be reset?

I have an object I create and stick in a collection:
public class Level
{
private string id;
private List<LevelTypes> usableLevelTypes; // LevelTypes is an enum
private List<BlockTypes> levelMapping; // BlockTypes is also an enum
public Level(string id, List<LevelTypes> levelTypes, List<BlockTypes> incomingBlocks)
{
this.id = id;
this.usableLevelTypes = levelTypes
levelMapping = incomingBlocks;
}
Stepping through this, I can see each item being set properly. The object is then placed in a HashSet.
Another class then iterates through the HashSet calling each item's overloaded .ToString() method.
At this point I have all relevant variables in this class on my watch list. Everything within the object called is set properly. "id", "levelMapping" and all other variables that I have not listed including other List<T>'s and int's contain their proper values except "usableLevelTypes", which is reported as being empty.
public override string ToString()
{
var s = new StringBuilder();
s.Append("ID: " + id);
s.Append(" Level Types: " + usableLevelTypes[0].ToString()); // At this point,
// this list should have at minimum one value in it. However, it is empty and
// will throw an exception stating as much.
return s.ToString();
}
At no point is .Clear() called on the usableLevelTypes List and it is read-only. How could it be reset when other lists within the same object are not?
You are not creating a copy of the list that you pass in to the Level constructor for usableLevelTypes. So whatever is happening to that outer list is going to happen to the list inside Level. Without seeing the calling code, I cannot tell you specifically what the problem is.

C#: Dictionary indexed by List

I try to write a program where Dictionary is indexed by List. (trust me i do, and yes there are option, but i like indexing by list). There is a minimal working (actually not working, only one last line which is a problem) example:
using System;
using System.Collections.Generic;
namespace test
{
class Program
{
static void Main(string[] args)
{
Dictionary<List<String>, int> h = new Dictionary<List<string>,int>();
List<String> w = new List<string> {"a"};
h.Add(w, 1);
w = new List<string>{"b"};
h.Add(w,2);
w = new List<string>{"a"};
int value = 0;
h.TryGetValue(w, out value);
Console.WriteLine(value+" "+h[w]);
}
}
if one debugs this program, he will clearly see that there two elements in h, but still these elements are not accessible via correct indexes --- h[w]. Am I wrong or is there something weird going on?
The problem with your app extends from the fact that:
new List<String> { "a" } != new List<String> { "a" }
Equality for lists checks to see if the two references refer to the same instance. In this case, they don't. You've instead created two Lists with the same elements...which doesn't make them equal.
You can fix the problem by creating a custom Equality Comparer:
public class ListEqualityComparer<T> : IEqualityComparer<List<T>>
{
public bool Equals(List<T> list1, List<T> list2)
{
return list1.SequenceEquals(list2);
}
public int GetHashCode(List<T> list)
{
if(list != null && list.Length > 0)
{
var hashcode = list[0].GetHashCode();
for(var i = 1; i <= list.Length; i++)
hashcode ^= list[i].GetHashCode();
return hashcode;
}
return 0;
}
}
And then passing that to the Dictionary constructor:
Dictionary<List<String>, int> h =
new Dictionary<List<string>,int>(new ListEqualityComparer<String>());
The problem is the index by List, what you are indexing by isn't the data in the list but you are essentially indexing by the memory pointer to the List (i.e the memory address of where this List is located).
You Created one list at one memory location, you then created a totally different list at a different memory location (ie when you create a new instance). The two lists are different even though they contain the same data, and this means you can add as many as you want to the dictionary.
One solution is Rather than indexing by List would be to index by String and use a comma separated List containing all the data in your list as an index.
This won't ever work for you, because List<T>'s Equals and GetHashCode methods don't consider the contents of the list. If you want to use a collection of objects as a key, you'll need to implement your own collection type that overrides Equals in such a way as to check the equality of the objects in the collection (perhaps using Enumerable.SequenceEqual.)
The Dictionary class uses reference comparison to look for the specified key, that's why even if the lists contain the same items, they are different.

Compare type of IEnumerable<T>

I have dictionary that gives me back a method according to the value passed in. Defined as so:
Dictionary<Type, IXmlWriterConverter>
I have now added a new function that which has the Key/type set to IEnumerable, so far so good.
But when I execute my unit test with a List containing two DataTables but the dictionary can not find the key e.g. my type conversion differs.
Why is that so? And what would be the right attempt to solve my problem?
Edit: Sorry here is the requested code ;-)
Function that generates the testvalues:
public IEnumerable<DataTable> CreateTestDataTableList()
{
var resultDataTable = new List<DataTable>();
resultDataTable.Add(CreateTestTable("testTable1", 2));
resultDataTable.Add(CreateTestTable("testTable2", 3));
return resultDataTable;
}
Function called by the unit test:
public void Write(XmlWriter xmlWriter, object value)
{
...
converter = FindConverter(value.GetType());
}
Function checking the dictionary:
public IXmlWriterConverter FindConverter(Type type)
{
if(Converters.ContainsKey(type))
{
return Converters[type];
}
return null;
}
2.Edit:
Code that adds the values to the Dictionary:
public void Add(IXmlWriterConverter xmlWriterConverter)
{
if(Converters.ContainsKey(xmlWriterConverter.InputType))
{
Remove(xmlWriterConverter);
}
Converters.Add(xmlWriterConverter.InputType, xmlWriterConverter);
}
The InputType is a readonly (get) property of the converter. I checked the type added to the dictionary and that was registered as IEnumerable, however when I checked on the typeof when passing in my list the type was List and not IEnumerable. I was told that this happens because I pass in the values as object.
This is a really code-stinky solution to me, and it cuts down on the efficiency, but you can also iterate through the GetInterfaces() method on Type, like this:
List<DataTable> l = new List<DataTable>();
var t = l.GetType();
var ints = t.GetInterfaces();
Then you could do a lookup on the type, and, if that doesn't work do a lookup on it's interfaces.
However, this feels like a terrible hack, which usually indicates that some more design work needs to be done. Is it not possible to put the List type in the dictionary? Is there no better way of doing this lookup?
Also, a note on doing dictionary lookups: It's more efficient to use the TryGetValue method, like this:
public IXmlWriterConverter FindConverter(Type type)
{
IXmlWriterConverter converter;
if( Converters.TryGetValue(type, out converter) )
{
return converter;
}
return null;
}
When you do it this way, it only does one lookup on the dictionary, whereas if you use ContainsKey it has to do two lookups.
Its a bit of hack but the only thing that gets into my mind when looking at your code, is to add an other generic write method:
public void Write<TValue>(XmlWriter writer, TValue value) {
// ...
}
This allows to identify the right type for IEnumerable and leaves the other Write method to not break any existing code.
Probably you are trying to retrieve List type and not IEnumerable (inheritance is not going to work in this context)
Please paste the code that does the lookup if you want more details and more certain answer :)
You should also post the code of where you are retrieving the test values. But based on what you've given, are you possibly using the non-generic IEnumerable to retrieve the values instead of the generic version that you're using to generate them?
The InputType is a readonly (get)
property of the converter. I checked
the type added to the dictionary and
that was registered as IEnumerable,
however when I checked on the typeof
when passing in my list the type was
List and not IEnumerable. I was told
that this happens because I pass in
the values as object.
Like they say on MythBusters, well, there's your problem! Even though List is-a IEnumerable the Type objects that represent each are definitely not the same. In programmer jargon, typeof(List) != typeof(IEnumerable). That is why the lookup is not working.

Categories