0

I've been doing some refactoring today and I encountered one interesting thing. I've extracted a method from a code which uses a cancellation token passed to it in order to check if the user did not cancel the parent task.

When I implemented this, I passed the cancellation token like this:

Object result = myClass.ExecuteSomething(token);

Now, when I refactored the ExecuteSomething method, by extracting methods in order to make the method look cleaner, the VS2012 designer created something like this:

public Object ExecuteSomething(CancellationToken token)
{
    // Some code here
    NewMethod(ref token);
}

private void NewMethod(CancellationToken token)
{
    // Some code here
}

Now, for me this looked strange, and I found that CancellationToken is a struct and not a class (And as I know passing a struct to a method is done by value and not by ref). I have tested the code without ref and the property of the token is updated correctly. This means that this property does not store the value but it is requesting it from somewhere using some reference. There is no source code for CancellationToken available so I wasn't able to find out how really this is done, and what is the difference here between ref and value passing of the token.

P.S. I thought this is more of a concept question than a problem so posted it here instead of StackOverflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.