As below code expressed, I want SomeMethod
- Have a parameter of some kind of int
- can accept null as parameter
if parameter is none null, it will use it's value, then update the value of argument variable in Caller2
void Caller1() { SomeMethod(null, ...); } void Caller2() { int argument = 123; SomeMethod(argument, ...); Debug.Assert(argument == 456); } void SomeMethod(SomeKindOfInt parameter, ...) { if (parameter != null) { // use the value of parameter; parameter = 456; // update the value of argument which is in Caller2 } }
Tried and declare:
- ref int can't accept null
- int? can't update argument in caller
- Create a custom Wrapper class of int did this, but is there a light way or does C# or .net have some build in tech?
- It's not good to split it into two methods because there's a big logic inside which is common whenever parameter is null or none null.