每当我尝试使用我的课程时,都会出现错误

时间:2019-02-08 03:19:26

标签: c#

我正在尝试编写一个使用预制列表生成侮辱的程序。当我尝试从类mStart,mMid或mEnd更改某些值时,出现错误。我究竟做错了什么?我是编码的新手,所以如果我犯了一个愚蠢的错误,请通知我。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace albertisnultsim
{
class Program
{
    public class startSent
    {
        public string[] listofstarters = new string[10];
    }

    public  class middle
    {
        public string[] listofmiddles = new string[10];
    }

    public class end
    {
        public string[] listofends = new string[10];
    }

    startSent mstart = new startSent();
    middle mMid = new middle();
    end mEnd = new end();

    static void Main(string[] args)
    {
    }
}
}

2 个答案:

答案 0 :(得分:3)

这就是我想要的:

namespace albertisnultsim
{
    class Program
    {
        public class startSent
        {
            public string[] listofstarters = new string[10];

        }

        public class middle
        {
            public string[] listofmiddles = new string[10];

        }
        public class end
        {
            public string[] listofends = new string[10];

        }


        static void Main(string[] args)
        {
            startSent mstart = new startSent();
            middle mMid = new middle();
            end mEnd = new end();

            mstart.listofstarters = new string[]{"setting1","1","setting2","apple"};
            Console.WriteLine(mstart.listofstarters[0]);
            Console.ReadKey();
        }
    }
}

答案 1 :(得分:1)

必须在特定的作用域中创建类的实例,而不仅仅是命名空间。由于这三个类都具有相同的有效属性,因此您也可以使用该事实来大大简化代码:

class Program
{
    public class Section
    {
        public string[] items = new string[10];
    }

    static void Main(string[] args)
    {
        Section start = new Section();
        Section middle = new Section();
        Section end = new Section();
    }
}