Problem converting string to char

I found this function toCharArray() that converts string to char and tried using it in my code

Code:

String MyString = "Arduino";
char ch[20];
MyString.toCharArray(ch,20);

void setup() {
  Serial.begin(9600);
}

void loop() {
}

I get this error message:

Arduino: 1.8.8 (Windows 10), Board: "Arduino/Genuino Uno"

sketch_jan13a:3:1: error: 'MyString' does not name a type

 MyString.toCharArray(ch,20);

 ^

exit status 1
'MyString' does not name a type

Can you please help me find out where I've got it wrong.
thanks!

1, you're using Strings, not strings. The char array you create is (almost) a string :wink:

Next, you call a method (function) outside any function (like setup() or loop()) which you can not do :wink:

septillion:
Next, you call a method (function) outside any function (like setup() or loop()) which you can not do :wink:

Thanks so much!! This helped me fix it.