c#:似乎不能把我的头绕在编译错误上

本文关键字:编译 错误 我的 不能 | 更新日期: 2023-09-27 18:12:33

我在"public static void PromoteEmployee(List employeeList, IsPromotable IsEligibleToPromote)"中的"PromoteEmployee"中面临编译问题

如果有人能给我提示一下我应该怎么做,我会很感激的。

编辑:

错误是:"不一致的可访问性:参数类型'程序。IsPromotable方法比Program.Employee方法更难访问。PromoteEmployee(列表,Program.IsPromotable)

class Program
{
    static void Main(string[] args)
    {
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee()
        {
            ID = 101,
            Name = "Test1",
            Salary = 5000,
            Experience = 5
        });
        empList.Add(new Employee()
        {
            ID = 101,
            Name = "Test2",
            Salary = 2000,
            Experience = 1
        });
        empList.Add(new Employee()
        {
            ID = 101,
            Name = "Test3",
            Salary = 4000,
            Experience = 4
        });
        IsPromotable isPromotable = new IsPromotable(Promote);
        Employee.PromoteEmployee(empList, isPromotable);
    }
    public static bool Promote(Employee emp)
    {
        if (emp.Experience >= 5)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    delegate bool IsPromotable(Employee empl);
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public int Experience { get; set; }
        public static void PromoteEmployee(List<Employee> employeeList, IsPromotable IsEligibleToPromote)
        {
            foreach (Employee employee in employeeList)
            {
                if (IsEligibleToPromote(employee))
                {
                    Console.WriteLine(employee.Name + " promoted");
                }
            }
        }
    }
}

c#:似乎不能把我的头绕在编译错误上

我编译你的程序得到的错误是:

(67:28) Inconsistent accessibility: parameter type 'Program.IsPromotable' is less accessible than method 'Program.Employee.PromoteEmployee(System.Collections.Generic.List<Program.Employee>, Program.IsPromotable)'

出现此错误是因为PromoteEmployeepublic,但IsPromotable是私有的

如果程序类外部的人想调用PromoteEmployee,他不能这样做,因为他不能创建Program.IsPromotable的实例,因为它是Program的私有实例。

——https://stackoverflow.com/users/424129/ed-plunkett

IsPromotable改为internalpublic,以便Program以外的其他人可以创建它。

也可以将PromoteEmployee设为私有

这是一个工作的dotnetfiddle。https://dotnetfiddle.net/4iuQjt

using System;
using System.Collections.Generic;
public class Program
{
    public static void Main(string[] args)
    {
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee()
        {
            ID = 101,
            Name = "Test1",
            Salary = 5000,
            Experience = 5
        });
        empList.Add(new Employee()
        {
            ID = 101,
            Name = "Test2",
            Salary = 2000,
            Experience = 1
        });
        empList.Add(new Employee()
        {
            ID = 101,
            Name = "Test3",
            Salary = 4000,
            Experience = 4
        });
        IsPromotable isPromotable = new IsPromotable(Promote);
        Employee.PromoteEmployee(empList, isPromotable);
    }
    public static bool Promote(Employee emp)
    {
        if (emp.Experience >= 5)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public delegate bool IsPromotable(Employee empl);
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public int Experience { get; set; }
        public static void PromoteEmployee(List<Employee> employeeList, IsPromotable IsEligibleToPromote)
        {
            foreach (Employee employee in employeeList)
            {
                if (IsEligibleToPromote(employee))
                {
                    Console.WriteLine(employee.Name + " promoted");
                }
            }
        }
    }
}