Elements of a C# List [closed]
I am using List.ElementAt(0) and List.ElementAt(1) to get the elements I need. How ever I don't want to hardcode 0 and 1 is there any other way in C#
List.First() will work. However, you really shouldn't be using ElementAt if you can help it -- it's slow. And since you're using a list you can indeed help it. Use the indexer instead (List[0], List[1]).
Use a variable?
List.ElementAt(i);
Yes, use a variable instead of hardcoding it. Also if it is a list you could directly use the indexer property: list[0]
精彩评论