Void methods like below does not work with system.debug... why ?

List<String> myStrings = new List<String> { 'one', 'two','Three','four'};
system.debug ('----' + mystrings) // works
system.debug('------' + mystring.sort()); // does not work because method return the void....

With the + you're saying that you'll be appending something to the existing string '-------'. You can't append a void to a string. You can't also debug a void. The name itself tells you that it's a void (doesn't return anything).

You should be calling the method and then outputing the result list:

List<String> myStrings = new List<String> { 'one', 'two','Three','four'};

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.