Well ... I have no idea what your "performance logic" problem might be but that code is um ... less than performant.
Look at what you are doing:
public MyResponse PostAuditData( MyResponse objMyResponse,int i)
{
MyResponse obj = objMyResponse == null ? new MyResponse(): objMyResponse;
if (objMyResponse == null)
obj.BeforeName = "BeforeName"+i;
else
obj.AfterName = "AfterName"+i;
return objMyResponse = obj;
}
So the first line checks it if is null, and if so, replaces it with a new instance.
And the second row checks again to see if it is null. Why? Why do two checks?
And at the end, you assign a value to a non-
ref
and non
output
parameter and return it. Why?
The first bit could be better done using the null-coalescing operator
MyResponse obj = objMyResponse ?? new MyResponse();
but combining them is simpler and more obvious:
public MyResponse PostAuditData(MyResponse objMyResponse, int i)
{
if (objMyResponse == null)
{
objMyResponse = new MyResponse();
objMyResponse.BeforeName = "BeforeName" + i;
}
else
{
objMyResponse.AfterName = "AfterName" + i;
}
return objMyResponse;
}
And it's not exactly an obvious way to do any of that ... it looks like somebody jumped straight into coding without thinking at all about what he was trying to do. I have no idea why the Audit class exists at all, or why it's one and only method isn't static.
To be honest, I'd start again from scratch after reading my homework question again, really carefully and thinking about the design for at least ten minutes before starting to code anything.