A characterization of Hilbert-Schmidt operators