I'm surprised the comment-region routine hasn't been mentioned. (Though I concede it may indicate I've missed something.) I've had the following line in my .emacs file for the better part of 20 years. It works well in most major programming modes I care about.
(global-set-key "\C-c\C-c" 'comment-region)
From the docs of 'comment-region'
Documentation: Comment or uncomment each line in the region. With just
C-u prefix arg, uncomment each line in region. Numeric prefix arg ARG
means use ARG comment characters. If ARG is negative, delete that many
comment characters instead. Comments are terminated on each line, even
for syntax in which newline does not end the comment. Blank lines do
not get comments.