I am working with some Legacy code. I general method that I usually would call like this:
Poco = MyMethod(Poco.Id) // Lookup PocoToReturn on Poco.Id and return PocoToReturn
are called in my Legacy like this:
MyLegacyMethod(Poco) // Lookup lookedUpPoco on Poco.Id, set Poco = lookedUpPoco and return (void)
The later work mostly but not always. I am struggling to understand when the later work and where it does not works (and should be fixed)
Consider the following execution of the code I are located at the end of this post:
Facade.AppleFacade.GetApple()is calledPersist.ApplePersist.GetAppleInfo(appleInfo)is calledinfo = new AppleInfo { Id = "newId", Name = "Test"}is executedGetAppleInfoin the persist returns toGetApplein the facade
Expected: Info.Id = "newId" and Info Name = "test"
Actual: Info.Id = "oldId" and Info Name = null
If I add ref to get MyStrangeMethod(ref Poco) the my Actual will be as Expected. I guess
I have 2-3 Questions:
- Why is
refneccessary in this case then other code wihtout ref is working without problems? - In general, what is the difference between using
refand using no prefix for Objects of different type? - (I think I know the answer to this) Why do I calls like
MyMethod(ref myPoco.myProperty)result in compile time error A property or indexer may not be passed as an out or ref parameter
Below the code example I mention above
namespace Facade
{
public class AppleFacade
{
public AppleInfo GetApple()
{
var appleInfo = new AppleInfo();
appleInfo.Id = "oldId";
_applePersist.LoadAppleInfo(appleInfo);
return appleInfo;
}
}
}
namespace Persist
{
public class ApplePersist
{
public void GetAppleInfo(AppleInfo info)
{
info = new AppleInfo
{
Id = "id",
Name = "test"
};
}
}
}