且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

由于保护级别,不能在同一名称空间中对公共类使用构造函数吗?

更新时间:2022-06-02 21:37:09

它可能看起来像:

public class ChannelHandler
{
    public ChannelName ChannelName { get; set; }
    public ParentId ParentId { get; set; }
    public List<ChildId> ChildIds { get; set; }
    public int Id;

    public ChannelHandler(ChannelName name, int id)
    {
        ChannelName = name;
        Id = id;
    }

    public ChannelHandler(ChannelName name, int id, ParentId parentid)
    {
        ChannelName = name;
        Id = id;
        ParentId = parentid;
    }

默认情况下,类的任何元素的访问级别为私人。您需要使构造函数成为公开内部,以便可以在类本身的外部进行访问。如果您看这篇文章 在这里解释了访问修饰符如何影响代码的可访问性级别。

By default, the access level of any element of a class is private. You need to make the constructors either public or internal so that can be accessed externally to the class itself. If you look at this article here it explains how access modifiers affect the accessibility levels of your code.

我进行的另一项更改是更改成员的大小写以满足 c#编码约定。请参见此处

The other change I made was to change the case of your members to meet c# coding conventions. See here