How is memory allocated when passing inherited class as param to a method using this keyword?
I am a junior developer and I am not sure if the sample code below is a good practice. I would like to know if there is a performance impact by passing current class instan开发者_运维百科ce as a parameter of a method. Also what could be the performance impact by passing it to several methods? Please advise on the code below.
Class X: Y
{
Z myObject = new Z();
myObject.MethodA( (Y)this);
}
Class Z
{
MethodA(Y y)
{
y.Mystream = Write Element
MethodB(ref y)
MethodC(ref y)
}
MethodB(ref Y y)
{
y.Mystream = Write Element
}
}
Class Y
{
Public XMLTextWriter Mystream = null;
}
In the first case (MethodA(this)
), a copy of the reference is passed by value to the method. This is blazingly fast. I guarantee it's not a bottleneck in your application.
In the second case (MethodB(ref y)
), the storage location of the reference is passed to the method. This is blazingly fast. I guarantee it's not a bottleneck in your application.
精彩评论