为啥出错,,

xiaotuzi 2013-06-17 04:03:50
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.cs 14 37 Chapter9
...全文
170 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
iceMung 2013-06-17
  • 打赏
  • 举报
回复
如果你只有这点代码的话,应该报如下错误:
未能找到类型或者命名空间名称“ClassExample16”(是否缺少using指令...)
显然你其他地方定义了ClassExample16,而你给出的代码中只有classexample16。
yyantifa 2013-06-17
  • 打赏
  • 举报
回复
看了半天,这个居然没有发现..
我姓区不姓区 2013-06-17
  • 打赏
  • 举报
回复
引用 楼主 maiyude 的回复:
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.cs 14 37 Chapter9
定义的是classexample16类, 而实例化的是ClassExample16类, 大小写都不一样,你确定写对了?
coobai 2013-06-17
  • 打赏
  • 举报
回复
不科学。。。。
xiaotuzi 2013-06-17
  • 打赏
  • 举报
回复
class classexample16 { ////这里是students public string[] students = { "Tom", "Alice", "Jack" };
bdmh 2013-06-17
  • 打赏
  • 举报
回复
students 在哪呢,是public的吗

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧