【发布时间】:2020-03-06 13:53:33
【问题描述】:
我仍在学习 C#,并且在理解方法中的内存分配方面遇到困难。 让我们想象一下我得到一些引用对象的情况,并且在我分配现有对象的方法中,将在内存中做什么?
我找到了Are instance methods duplicated in memory for each object?,但如果我描述的话,我不太清楚。 任何其他参考将不胜感激。
public class ClassToBeAssigned : IClassToBeAssigned {}
public interface IClassToBeAssigned{}
public class AllocatingClass
{
private ClassToBeAssigned testAssigment;
// Just as example
void Main()
{
// new allocation in memory
testAssigment = new ClassToBeAssigned();
Assign(testAssigment);
}
// create here copy of context by assigned
void Assign(IClassToBeAssigned assigned)
{
// What will happend now if there are 4x method calls ?
DoSomething(assigned);
DoSomething(assigned);
DoSomething(assigned);
DoSomething(assigned);
}
void DoSomething(IClassToBeAssigned assignIt)
{
// What is happening here in memory allocation for that reference each call ?
IClassToBeAssigned dealWithIt = assignIt;
}
}
我只是对那里发生的事情有点困惑,我也找到了很多信息,但没有针对这个特定问题。
【问题讨论】:
标签: c# .net memory-management allocation