I wrote this code (Only the first line is important):
public void InsertIntoBaseElemList(ref List<XElem> List, XElem Element)
{
for (int index = 0; index < List.Count; index++) {
if (List[index].Position < Element.Position && index + 1 == List.Count) {
List.Add(Element);
} else if (List[index].Position > Element.Position) {
List.Insert(index, Element);
}
}
}
This method basically inserts an element of type XElem into a list of type XElem.
(Both parameters must have the same type. XElem in this case)
I have multiple of these lists but they don't have the same Type.
In order to allow inserting elements of type YElem into a list of type YElem, I'd have to copy this method and change the parameter types.
Is it possible to write a single method which can handle multiple types as a parameter, which guaranties parameter 1 and parameter 2 to be of the same type?
I read about Generic Types, but I couldn't make it work.
refyou have there seems pointless.refis only "needed" forvalue types. Also theforloop doesn't make much sense IMO (see my answer for explanation)...