首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

为什么出错,

2013-07-01 
为啥出错,,using Systemusing System.Collections.Genericusing System.Linqusing System.Textnamespa

为啥出错,,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapter9
{
    class Class1
    {
        private string[] students = { "Tom", "Alice", "Jack" };
        static void Main()
        {
            var obj = new ClassExample16();
            for (int i = 0; i < obj.students.Length; i++)
            {
                Console.WriteLine(obj[i]);
                Console.ReadKey();
            }
        }

    }

    class classexample16
    {
        public string[] students = { "Tom", "Alice", "Jack" };
        public string this[int studentNo]
        {
            get
            {
                return students[studentNo];
            }
            set
            {
                students[studentNo] = value;
            }
        }
    }
}

编译后,错误1“Chapter9.ClassExample16.students”不可访问,因为它受保护级别限制E:\CSharp4_DefinitiveGuide_Source4\Chapter9\Class1.cs1437Chapter9

[解决办法]

引用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapter9
{
    class Class1
    {
        private string[] students = { "Tom", "Alice", "Jack" };
        static void Main()
        {
            var obj = new ClassExample16();
            for (int i = 0; i < obj.students.Length; i++)


            {
                Console.WriteLine(obj[i]);
                Console.ReadKey();
            }
        }

    }

    class classexample16
    {
        public string[] students = { "Tom", "Alice", "Jack" };
        public string this[int studentNo]
        {
            get
            {
                return students[studentNo];
            }
            set
            {
                students[studentNo] = value;
            }
        }
    }
}

编译后,错误1“Chapter9.ClassExample16.students”不可访问,因为它受保护级别限制E:\CSharp4_DefinitiveGuide_Source4\Chapter9\Class1.cs1437Chapter9


定义的是classexample16类,
而实例化的是ClassExample16类,
大小写都不一样,你确定写对了?

热点排行