gpt4 book ai didi

c# - 使用 Monodevelop 在 C# 中的文本框的属性行

转载 作者:行者123 更新时间:2023-11-30 17:55:36 25 4
gpt4 key购买 nike

我在 monodevelop 中编写一个 c# winform 应用程序,我必须使用属性 Lines 将多行文本框的几行内容设置为特定的字符串值。所以我写了这段代码,在 Visual Studio 中不会产生异常并且可以正常工作,但在单声道中不起作用:

public string[] newLines = new string[36]; 

public void Main_Form_Load(object sender, EventArgs e)
{
newLines[3] = "</CsOptions>";
textBox1.Lines = newLines;
}

特别是 istruction textBox1.Lines = newLines;给出一个空引用异常,当我删除它时,异常过期。

最佳答案

数组将每个元素都初始化为其默认值。在您的例子中,每个元素(属于 string 类型)都被初始化为 null,这与 不同空字符串 "".

尽管 .NET 和 mono 应该 兼容,但在这种特殊情况下它们可能不兼容。两者之间在处理 TextBox.Lines 属性方面可能存在一些差异,一种可能的情况是 mono 不检查 null 而 .NET 会检查。

您应该避免像那样使用null 并始终初始化数组。例如:

for(int i = 0; i < newLines.Length; i++){
newLines[i] = "";
}

关于c# - 使用 Monodevelop 在 C# 中的文本框的属性行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14936682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com