C#对象修改 [英] C# object modification
问题描述
的我觉得这真的是一个非常蹩脚的问题,但我猜 SO
我将获得答案的速度比通过谷歌搜索它自己:)
让我们说我有一些类构造函数:
公共类识别TestClass {
私人只读IRepository库;
公众的TestClass(IRepository库)
{
this.repository =库;
//这里是不明显线 - 它会调用一些
//方法,它改变了
//库对象的内部内容。
repository.AddSomething(...);
}
}
的现在的
IRepository sqlRepository =新sqlRepository(...);
TestClass中的TestClass =新识别TestClass(sqlRepository);
1)我M不善于值/引用 C#
传递 - 所以请能有人给一个一步一步在这种情况下,会发生什么情况的说明
2)是否 sqlRepository
对象被修改的(我假设不)的,是有办法使构造的TestClass
修改(我知道这是邪恶的,只是我知道)?
3)将在 repository.AddSomething(...)
和 this.repository.AddSomething(...)
在构造线有同样的效果,为什么?
< STRONG> 4)什么影响只读
对在这个例子中,库变化的尝试?
-
在这种情况下,引用
sqlRespository
传递到构造函数。 -
还没有。当你调用
AddSomething()
,该方法确实修改原始实例。我们仍然只有一个对象,每个人都接触的那一个。这是一个重要的区别,既然每个人都有同一个对象的引用,他们所做的任何更改将作出的到该对象的。但是,如果他们简单地覆盖了不同的对象变量,即只能在本地适用。请记住,变数就像插槽。我们可以有大量插槽,所有的点回到同一个对象,但有一个指针代替一个插槽的不同的的对象不影响其他插槽。 -
的如果的首次设置
this.repository =库
则都被称为当地的参数库
键,名为库
成员变量(场)持有同一对象的引用。我们使用这一点。
要清楚,我们指的成员变量(场)和的不的局部参数或变量。 -
只读
指成员变量/字段只能从构造进行分配。想想成员变量作为一个插槽,我们可以把一个对象。如果插槽只读
,这意味着它只能在构造函数调用期间被填充。之后,它不能被替换。但这的不的意思是它里面的对象是有点只读。对象仍可以修改,它只是不能被另一个对象来代替 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.
醇>
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?
这篇关于C#对象修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!