A \chardef
token is not a character, but a command to print a character.
As such, it is not allowed inside \csname...\endcsname
where only character tokens (after macro expansion) are permitted.
The fact that \edef\foo{~}
yields ~
is because a \chardef
token is unexpandable. Similarly for \message{~}
.
The fact that TeX assigns special internal codes to active characters is irrelevant. The relevant aspect is whether an active character's definition is a macro or not. If it is a macro, it is expanded; otherwise it isn't and it's behavior depends on the context.
So, for instance, if you have
\chardef~="16
\csname\ifnum~="16 \string~\else foo\fi\endcsname
is perfectly good and would end up with \~
. But it's not really "using ~
inside \csname...\endcsname
, of course.
On the other hand, \&
is defined by \chardef\&="26
and \&
is not allowed ”naked” inside \csname...\endcsname
, exactly like ~
would be if it's a \chardef
token.
Another similar problem is with implicit character tokens. If you do
\let~=a
you're not allowed to use ~
inside \csname...\endcsname
as well.