Skip to content

Instantly share code, notes, and snippets.

@lordlycastle
Last active January 17, 2019 16:21
Show Gist options
  • Save lordlycastle/12116377299c38f063ffa0b68bc5d6f3 to your computer and use it in GitHub Desktop.
Save lordlycastle/12116377299c38f063ffa0b68bc5d6f3 to your computer and use it in GitHub Desktop.
Function to remove null values from a main list and removes values from other relating lists at same index.
// Doesn't work bcz ref and params doens't work. I now this was a concious decision by developers.
// This doesn't work. But it is kinda what 'pythonic'.
private void CheckForNull<T, T2>(ref List<T> listOfObjs, ref params List<T2>[] otherLists)
{
for (int i = listOfObjs.Count; i >= listOfObjs.Count; i--)
{
var o = listOfObjs[i];
if (o == null)
{
listOfObjs.RemoveAt(i);
foreach (List<T2> otherList in otherLists)
{
otherList.RemoveAt(i);
}
}
}
}
// Way that does work but is not generic. Want to be generic so I don't need to write functions for different numbers of arguments.
private void CheckForNull<T1, T2, T3>(ref List<T1> listOfObjs, ref List<T2> otherList, ref List<T3> anotherList)
{
for (int i = listOfObjs.Count; i >= listOfObjs.Count; i--)
{
var o = listOfObjs[i];
if (o == null)
{
listOfObjs.RemoveAt(i);
otherList.RemoveAt(i);
anotherList.RemoveAt(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment