How to get the Contact Id while creating the Task using Apex Trigger

How to get the Contact Id while creating the Task using Apex Trigger
while creating the task to contact, its possible to get the contact information from Task WhoId or WhatId field and below is the example code: