我正在按相反的字母顺序对文本框的行进行排序,我的代码工作正常:
string[] temp = textBox.Lines;
Array.Sort(temp);
Array.Reverse(temp);
textBox.Lines = temp;
但我很困惑为什么 Visual Studio 不允许我这样做:
Array.Sort(textBox.Lines);
Array.Reverse(textBox.Lines);
大部分时间我都在尝试弄清楚 C# 的微妙之处,因为我对它还是个新手。
编辑:第二个片段没有出错,但它没有执行任何代码(即似乎没有做任何事情)。
最佳答案
下面是 TextBox.Lines
的几行 getter:
public string[] Lines
{
get
{
string text = Text;
ArrayList list = new ArrayList();
...
...
return(string[]) list.ToArray(typeof(string));
}
您将获得一个新集合,其中包含来自 TextBox
的数据的副本。您实际上并未对 TextBox
中的数据进行排序。
当 void Array.Sort
对从该属性返回的数组进行排序时,原始 TextBox
保持不变。
关于c# - 可以对某些数组进行排序但不能对其他数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23523550/