在项目中经常看到判断null的判断,对这个有无必要执怀疑,有空就测试了一下,在列表创建之后就已不是空,所以完全无必要
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
IList<int> list1 = new List<int> { 1, 2 };
IList<int> list2 = new List<int> { 3, 5 };
if (list == null)
{
Console.WriteLine("list is null");
}
else
{
Console.WriteLine("list is not null");
}
for(int i = 0; i < list1.Count; i++)
{
list2.Add(list1[i]);
}
for(int i = 0; i < list2.Count; i++)
{
Console.WriteLine(list2[i]);
}
Console.Read();
}
}
}
结果:
list is not null
3
5
1
2
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Collections.ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("mango");
fruits.Add("apple");
fruits.Add(1); // 这个转换时会报错, int32无法转换为string类型
IEnumerable<string> query = fruits.Cast<String>().OrderBy(fruit => fruit).Select(fruit => fruit);
foreach (string fruit in query)
{
Console.WriteLine(fruit);
}
Console.Read();
}
}
}