比方法更难接近

本文关键字:难接近 方法 | 更新日期: 2023-09-27 18:14:27

class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            student s = new student();
            foreach (var item in p.ab())
            {
                Console.WriteLine(item.id+item.name+item.fname);  
            }
        }
        public  List<student> ab() 
        {
            List<student> l = new List<student>()
              {
                 new student{id=1,name="hjk",fname="xyz"},
              };
            return l;
         }
        class student
        {
            public int id { get; set; }
            public String name { get; set; }
            public String fname { get; set; }
        }

访问不一致:

返回类型"System.Collections.Generic.List"

<ConsoleApplication7.Program.ab()>

比方法更难接近

在类声明中添加public ----返回类型是List<student>比method(method是public)更难访问,所以你需要将type设置为public

public class student
    {
        public int id { get; set; }
        public String name { get; set; }
        public String fname { get; set; }
    }

你的main方法应该被包装在公共类

学生应该是公共的

检查下面的代码片段

using System;
using System.Collections.Generic;
public class Program
    {
        public static void Main(string[] args)
        {
            Program p = new Program();
            student s = new student();
            foreach (var item in p.ab())
            {
                Console.WriteLine(item.id+item.name+item.fname);  
            }
        }
        public  List<student> ab() 
        {
            List<student> l = new List<student>()
              {
                 new student{id=1,name="hjk",fname="xyz"},
              };
            return l;
         }
}
 public class student
        {
            public int id { get; set; }
            public String name { get; set; }
            public String fname { get; set; }
        }

希望能有所帮助