Get Selected Feature from ArcMap

Get selected feature from ArcMap.

This blog defines how to get selected feature from ArcMap.

        IMxDocument pMxDoc = m_application.Document as IMxDocument;
        IMap pMap = pMxDoc.FocusMap;
        IEnumFeature pEnumFeat = pMap.FeatureSelection as IEnumFeature;

        pEnumFeat.Reset();

        IFeature pFeat = pEnumFeat.Next();

        if (pFeat == null)
        {

            MessageBox.Show("No Feature Selected.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

            System.Runtime.InteropServices.Marshal.ReleaseComObject(pEnumFeat);

            return;

         }
        IFields pFields;

        //Retrieving alla Fields value
        IEnumFeatureSetup enumFeatSetup = (IEnumFeatureSetup)pEnumFeat;
        enumFeatSetup.AllFields = true;
        while (pFeat != null)
        {

            pFields = pFeat.Fields;

            int indx = pFields.FindField("ID");

            if (indx == -1)

            {

                MessageBox.Show("ID field not found in selected feature ", "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);

                pFeat = pEnumFeat.Next();
                continue;
             }
             object valu = pFeat.get_Value(indx);
            if (valu != null)
            {

                //Do something

            }

             pFeat = pEnumFeat.Next();
        }

        //Release Com object
 
        System.Runtime.InteropServices.Marshal.ReleaseComObject(pEnumFeat);
  

    }