题目
In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out.
Example
ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b"}) => {1, 2}
ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", 0, 15}) => {1, 2, 0, 15}
ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", "aasf", "1", "123", 231}) => {1, 2, 231}
代码
using System.Collections;
using System.Collections.Generic;
public class ListFilterer
{
public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems)
{
foreach(var obj in listOfItems){
if(obj is int){
yield return (int)obj;
}
}
}
}
参考资料
- yield:https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/yield
- is:https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/is