如何在C#程序中修复CS0200?
我知道相应的微软支持我不应该使用readonly
,但我没有。
尝试更新字符串中各个字符的行上会显示错误 CS0200:
“无法为属性或索引器分配值”。
private string Start { set; get; }
private string Middle { set; get; }
private string Finish { set; get; }
private string Alpha { set; get; }
public Form1()
{
InitializeComponent();
Alpha = "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя";
}
private void button1_Click(object sender, EventArgs e)
{
Start = textBox1.Text;
Middle = "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя";
for(int i = 0; i < Start.Length; i++ )
{
for (int j = 0; j < Middle.Length ;j++)
{
if (Start[i] == Middle[j])
{
char el = Middle[i];
Middle[i] = Middle[j]; // CS0200
Middle[j] = el; // CS0200
}
}
}
}
I know that accordingly Microsoft support I should not use readonly
, but I didn't.
The error CS0200 is shown on lines trying to update individual characters in the string:
"unable to assign value to property or indexer".
private string Start { set; get; }
private string Middle { set; get; }
private string Finish { set; get; }
private string Alpha { set; get; }
public Form1()
{
InitializeComponent();
Alpha = "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя";
}
private void button1_Click(object sender, EventArgs e)
{
Start = textBox1.Text;
Middle = "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя";
for(int i = 0; i < Start.Length; i++ )
{
for (int j = 0; j < Middle.Length ;j++)
{
if (Start[i] == Middle[j])
{
char el = Middle[i];
Middle[i] = Middle[j]; // CS0200
Middle[j] = el; // CS0200
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串是不可变的,你不能像这样改变它们。
使用
StringBuilder
来创建另一个具有更改字符的字符串,例如:或者更好 - 从头开始将
Middle
声明为StringBuilder
,这将减少字符串->字符串生成器 ->字符串转换。如果您只交换字符并且从不删除/添加任何字符,那么字符数组也是一个选项。
Strings are immutable, you cannot change them like this.
Use
StringBuilder
to make another string with changed characters, for example:or even better - declare
Middle
asStringBuilder
from the beginning, this will reduce string -> StringBuilder -> string conversions.Char array is also an option if you only swap characters and never remove / add any.