CDs, DVDs & PenDrives containing viruses or malwares make use of the AutoRun feature in Windows to infect your computer as soon as they were loaded. AutoRun was introduced with Windows 95 so a CD or DVD that contained a file called autorun.inf would start up when it was loaded in the CD drive.
AutoPlay was added in Windows XP,Windows Vista and Windows 7. AutoPlay brings up a dialog box when any removable media like CDs, external hard drives or USB memory sticks are attached to the computer. The dialog box gives the user a list of possible actions like open as a folder, cancel, autorun a file, and more.