作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有点困惑为什么我可以从 linq 查询访问私有(private)变量?
访问是在 PropertyName 方法中访问 _propertyName 变量。我想因为它是私有(private)的,所以我无法访问它?
public class ObjectGraph
{
private readonly string _propertyName;
public string PropertyName
{
get
{
// ermmm, not sure why this Select can access _propertyName???
var parents = string.Join("/", Parents.Select(p => p._propertyName));
return string.Format("{0}/{1}", parents, _propertyName);
}
}
public ObjectReplicationContext Source { get; private set; }
public int ClosestParentId { get; set; }
public bool TraverseChildren { get; set; }
public List<ObjectGraph> Parents { get; set; }
public ObjectGraph(object source, string propertyName)
{
Source = new ObjectReplicationContext(source);
_propertyName = propertyName;
Parents = new List<ObjectGraph>();
TraverseChildren = true;
}
}
最佳答案
一个类总是可以访问它自己的私有(private)成员。 lambda 只是类中存在的匿名回调方法;它是由 C# 编译器提供的语法糖。
想象一下你有这个
private static string GetProperyName(ObjectGraph obj) {
return obj._propertyName;
}
public string PropertyName
{
get
{
// ermmm, not sure why this Select can access _propertyName???
var parents = string.Join("/", Parents.Select(GetProperyName));
return string.Format("{0}/{1}", parents, _propertyName);
}
}
这等同于您的代码。
关于c# - 对从 Linq 查询访问私有(private)变量感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23050116/
我正在尝试学习Rust。我正在阅读一本书online,该书实现了unix程序cat。现在,我试图读取作为像cargo run file1.txt file2.txt这样的参数传递的文件的内容,但是程序
我在 GHC 8.0.1 中遇到了一个带有种类索引 (?) GADT 的奇怪情况,其中在类型与种类签名中引入 foralls 会产生不同的类型检查行为。 考虑以下数据类型: {-# LANGUAGE
我正在使用 Perl 5.10 开发应用程序,HTML::Mason和 Apache 2.2。这是我第一次在大型项目中使用 Perl 5.10。我每隔一段时间就会出现奇怪的行为。应用程序因一个非常奇怪
我正在尝试将文件上传到aws中的rust中,因为我使用的是 rusoto_s3 的s3 rust客户端,当这些部分从单个线程发送时,我设法使分段上传代码正常工作不是我想要的,我想上传大文件,并且希望能
我是一名优秀的程序员,十分优秀!