- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 159字
- 2025-04-04 18:04:55
Working with the IEnumerable generic interface
Next, in between the set of curly braces beneath the line beginning with protected void Button1_Click..., the first thing that we will do is create an array of names. For this, enter the following:
IEnumerable<string> names = new string[] { "john", "job", "janet", "mary", "steve" };
Let's name it names, and then say, create a new string array. Then, to specify the initializer list, we enter a series of names in quotes, and close this with a semicolon.
Now notice that, on the left-hand side, we have IEnumerable. This is a generic interface. As you can see, the new string array in this line can be created this way because it's possible to take an array then step through it, so that each entry inside the array is a string. So, it's IEnumerable: we can list values within it, and each value to be listed is a string. To enumerate means to list.