April 2, 2009
C# Lists
Use of Lists in C#
What you will learn here:
1) Syntax is “List
2) How to add items to a List
3) How to use a “foreach” statement to loop through a list
4) Our specific example is a subroutine (method) that receives a List of numbers, and returns only the odd numbers in that List.
This video is a solution to a “quiz” I was given in an interview. A developer gave me this method signature, and asked me to scratch out the code to accomplish returning all the odd numbers in the list (at first I thought he said all the odd numbered items in the list).
|
I had a “brain cloud” when doing this demo. I was thinking I could divide by 2 to find out if the number was even. I totally forgot about “modulo” division, which is what my subconscious was thinking. If you use a backslash instead of a forward slash, then the remainder will be returned. If you do modulo division dividing by 2, and the remainder is zero, then you have an even number, otherwise an odd number. That’s a more elegant way to do the odd/even test than what I did in the video below.
Code for you to copy:
|
Video Demonstration:
Filed under C# Lists by Administrator

