C# 对象修改
我认为这确实是一个非常蹩脚的问题,但我想在 SO
上,我会比自己通过谷歌搜索更快地收到答案:)
假设我有一些课程,其中有构造函数:
public class TestClass {
private readonly IRepository repository;
public TestClass(IRepository repository)
{
this.repository = repository;
// Here is the non-obvious line - it invokes some
// method, which changes the internal contents of
// the repository object.
repository.AddSomething(...);
}
}
现在:
IRepository sqlRepository = new SqlRepository(...);
TestClass testClass = new TestClass(sqlRepository);
1) 我不擅长在 C#
中传递值/引用 - 所以请有人给一个 逐步解释本例中发生的情况。
2) sqlRepository
对象是否被修改(我假设没有),是否有办法创建 TestClass 的构造函数code> 修改它(我知道这是邪恶的,只是为了我自己知道)?
3) 构造函数中的 repository.AddSomething(...)
和 this.repository.AddSomething(...)
行是否具有同样的效果,为什么?
4) 在此示例中,readonly
对此存储库更改的尝试有何影响?
I think this is really a very lame question, but I guess on SO
I will receive the answer faster than by googling it myself :)
Let's say I have some class with a constructor:
public class TestClass {
private readonly IRepository repository;
public TestClass(IRepository repository)
{
this.repository = repository;
// Here is the non-obvious line - it invokes some
// method, which changes the internal contents of
// the repository object.
repository.AddSomething(...);
}
}
And now:
IRepository sqlRepository = new SqlRepository(...);
TestClass testClass = new TestClass(sqlRepository);
1) I'm not good at value / reference passing in C#
- so please could someone give a step-by-step explanation of what happens in this case.
2) Does the sqlRepository
object get modified (I assume not) and is there a way to make the constructor of TestClass
modify it (I know it's evil, just for me to know)?
3) Would the repository.AddSomething(...)
and this.repository.AddSomething(...)
lines in the constructor have the same effect and why?
4) What effect does readonly
have on the attempts of the repository changes in this example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,对
sqlRespository
的引用将传递到构造函数中。还没有。当您调用
AddSomething()
时,该方法确实正在修改原始实例。我们仍然只有一件物品,而每个人都在触摸那个物品。这是一个重要的区别,因为每个人都拥有对同一对象的引用,所以他们所做的任何更改都将针对该对象进行。但是,如果他们只是用不同的对象覆盖该变量,则这只适用于本地。请记住,变量就像槽。我们可以有很多槽,它们都指向同一个对象,但是用指向不同对象的指针替换一个槽不会影响其他槽。如果您首先设置
this.repository =repository
,则本地参数称为repository
,成员变量(字段)称为存储库
保存对同一对象的引用。我们使用this.
来明确我们指的是成员变量(字段),而不是本地参数或变量。readonly
表示成员变量/字段只能从构造函数分配。将成员变量视为我们可以放置对象的槽。如果槽是只读的,则意味着它只能在 ctor 调用期间填充。之后就无法更换了。这并不意味着它内部的对象在某种程度上是“只读”的。该对象仍然可以修改,只是不能被另一个对象替换。In this case the reference to
sqlRespository
is passed into the ctor.Not yet. When you call
AddSomething()
, that method is indeed modifying the original instance. We still only have one object and everyone is touching that one. It's an important distinction that since everyone has a reference to the same object, any changes they make will be made to that object. However, if they simply overwrite that variable with a different object, that only applies locally. Remember, variables are like slots. We can have lots of slots that all point back to the same object, but replacing one slot with a pointer to a different object doesn't affect the other slots.If you first set
this.repository = repository
then both the local parameter calledrepository
and member variable (field) calledrepository
hold a reference to the same object. We usethis.
to be clear we mean the member variable (field) and not the local parameter or variable.readonly
means that member variable / field can only be assigned from the constructor. Think of the member variable as a slot where we can put an object. If the slot isreadonly
, that means it can only be filled during the ctor call. After that it can't be replaced. That does not mean that the object inside of it is somehow "read-only". The object can still be modified, it just can't be replaced by another object.