MonoDevelop C# - 编译类文件

时间:2018-03-03 20:05:39

标签: c# linux class monodevelop

我正在尝试在MonoDevelop版本5.0.1.1中编译基本测试类。请参阅以下代码:

using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;

namespace testproject
{
    public class TestClass
    {
        public TestClass
        {
            string name;
            public string Name
            {
                get {return name;}
                set {name = value;}
            }
            int[] integers;
            public int this[int i]
            {
                get {if (i < 5) {return integers[i];} else {return -1;}}
                set {if (i < 5) {integers[i] = value;}}
            }
            public TestClass(string _name)
            {
                name = _name;
            }
            public override string ToString ()
            {
                string output = name + ":";
                for (int i = 0; i < 5; i++)
                {
                    if (i != 4) {output += " " + i.ToString() + ",";}
                    else {output += " " + i.ToString();}
                }
                return output;
            }
        }
    }
}

这是我在Linux上试用MonoDevelop时创建的一个非常基础的测试类(我刚从Windows上的VS2017切换)。当我尝试编译时,我收到以下错误:

  

/家庭/主/单声道-CS /项目/测试项目/测试 -   project / TestClass.cs(3,3):错误CS1519:类中的标记'{'无效,   struct或接口成员声明(CS1519)(测试项目)

  

/家庭/主/单声道-CS /项目/测试项目/测试 -   project / TestClass.cs(1,1):错误CS1022:类型或命名空间定义,   或预期的文件结束(CS1022)(测试项目)

我仔细检查过,所有花括号都有适当的对应。有什么见解吗?

全部谢谢!

1 个答案:

答案 0 :(得分:1)

  1. 删除public TestClass,这是重复添加嵌套TestClass
  2. 初始化integers否则您将收到运行时错误。(感谢@Ron Beyer提醒)
  3. 下面

    namespace testproject
    {
        public class TestClass
        {
            string name;
            public string Name
            {
                get {return name;}
                set {name = value;}
            }
            int[] integers;
            public int this[int i]
            {
                get {if (i < 5) {return integers[i];} else {return -1;}}
                set {if (i < 5) {integers[i] = value;}}
            }
            public TestClass(string _name)
            {
                name = _name;
                integers = new int[100];
            }
            public override string ToString ()
            {
                string output = name + ":";
                for (int i = 0; i < 5; i++)
                {
                    if (i != 4) {output += " " + i.ToString() + ",";}
                    else {output += " " + i.ToString();}
                }
                return output;
            }
    
        }
    }