小编Pet*_*ete的帖子

列表<>容量返回的项目多于添加的项目

有几个属性List<T>似乎与列表中的项目数相关 - Capacity,Count(作为属性和方法存在).与Array刚刚 相比,这非常令人困惑Length.

我正在使用,List.Capacity但它给出了意想不到的结果:

List <string> fruits = new List<string>();
fruits.Add("apple");
fruits.Add("orange");
fruits.Add("banana");
fruits.Add("cherry");
fruits.Add("mango");
Console.WriteLine("the List has {0} items in it.", fruits.Capacity);
Run Code Online (Sandbox Code Playgroud)

当我运行此控制台时,控制台显示:

the List has 4 items in it.

我不明白为什么它显示Capacity8,当我只添加5项.

c# collections list

13
推荐指数
4
解决办法
1万
查看次数

麻烦三元运算符/速记如果

if ((file.Exists) ? 
lblresults.Text = "the file is there" : 
lblresults.Text = "the file is not there");
Run Code Online (Sandbox Code Playgroud)

我一直得到错误说明不能隐含地将字符串转换为bool

任何帮助都会很棒,谢谢.

c# asp.net if-statement

0
推荐指数
1
解决办法
521
查看次数

标签 统计

c# ×2

asp.net ×1

collections ×1

if-statement ×1

list ×1