C# 使用访问修饰符后无法访问类属性?

C# 使用访问修饰符后无法访问类属性?,c#,C#,我只是在学习c。还有一本学习书,一些在线教程和一些关于访问修改器的博客文章 当我试图写一些代码时,我遇到了一些问题==>如下 using System; using System.Collections.Generic; namespace ConsoleApp1 { public class Employees { public int Id { get; set; } protected string Name { get; set; } public int Sa

我只是在学习c。还有一本学习书,一些在线教程和一些关于访问修改器的博客文章

当我试图写一些代码时,我遇到了一些问题==>如下

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
public class Employees
{
    public int Id { get; set; }
    protected string Name { get; set; }
    public int Salary { get; set; }
    public int ServiceYear { get; set; }

    void CheckEmployeePromotion(List<Employees> employees)
    {
        foreach (var employee in employees)
        {
            if (employee.ServiceYear >= 5)
            {
                Console.WriteLine(employee.Name + " Promoted");
            }
        }
    }
}

public class nayan
{
    Employees hasan = new Employees();
    hasan.Id = 1;
}

public class anothereclass : Employees
{
    Employees n1 = new Employees();
    n1.Id = 2;
}


public class Program
{
    public static void Main(string[] args)
    {
        Employees jamal = new Employees();
        jamal.Id = 23;
    }
}
}

然后我的问题是,为什么我无法访问其他类中的Employees的属性(也就是具有继承自Employees类的类)。我的问题是为什么不是解决方案

您只能在方法、构造函数或属性中设置实例的成员。所以我假设你想要这个:

class Nayan
{
    Employees n1 = new Employees();

    public Nayan()
    {
        n1.Id = 2;
    }
}

还考虑命名约定,这是命名类PascalCase。

您只能在方法、构造函数或属性中设置实例的成员。所以我假设你想要这个:

class Nayan
{
    Employees n1 = new Employees();

    public Nayan()
    {
        n1.Id = 2;
    }
}

还考虑命名约定,这是命名类PascalCase。< /p>编译器给出的错误是什么?可能忘记了用方法/构造函数包装它吗?编译器给的错误是什么?可能忘了用方法/构造函数包起来吗?