相关疑难解决方法(0)

当C#在同一个包含的类中时,为什么以及如何允许访问类本身之外的私有变量?

我不知道问题是否足够描述,但为什么以及如何存在这种行为?:

public class Layer
{
    public string Name { get; set; }

    private IEnumerable<Layer> children;
    public IEnumerable<Layer> Children
    {
        get { return this.children.Where ( c => c.Name != null ).Select ( c => c ); }
        set { this.children = value; }
    }

    public Layer ( )
    {
        this.children = new List<Layer> ( ); // Fine

        Layer layer = new Layer ( );
        layer.children = new List<Layer> ( ); // Isn't .children private from the outside?
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以访问layer.Children …

.net c# compiler-construction class private-members

20
推荐指数
2
解决办法
6437
查看次数

为什么在传递与方法C#的参数相同的类型时,类的私有字段是可见的?

让我们看看这堂课:

    public class Cluster
    {
        private List<Point> points; //private field

        public float ComputeDistanceToOtherClusterCLINK(Cluster cluster)
        {
            var max = 0f;
            foreach (var point in cluster.points) // here points field are accessible
            {
                  .......
            }
            return max;
        }
    }
Run Code Online (Sandbox Code Playgroud)

为什么我可以访问私人领域?

我可以使用此功能还是可能是不好的做法?

c# private-members

5
推荐指数
2
解决办法
262
查看次数

标签 统计

c# ×2

private-members ×2

.net ×1

class ×1

compiler-construction ×1