Share via


How to: Implement Interface

Use this procedure to perform the Implement Interface IntelliSense operation. For more information, see Implement Interface.

To implement an interface in C# with explicit declarations using IntelliSense.

  1. Create a console application.

  2. Place the cursor after the class Program declaration.

  3. Type : IComparable so the declaration becomes class Program : IComparable.

  4. Activate the smart tag under IComparable. The following two options will appear:

    • Implement interface 'IComparable'.

    • Explicitly implement interface 'IComparable'.

  5. Choose Explicitly implement interface 'IComparable'.

IntelliSense then adds an IComparable.CompareTo method from the IComparable interface to the Program class:

   #region IComparable Members
   int IComparable.CompareTo(object obj)
   {
      throw new Exception("The method or operation is not implemented.");
   }
   #endregion

To implement an interface in C# with implicit declarations using IntelliSense.

  1. Create a console application.

  2. Place the cursor after the class Program declaration.

  3. Type : IComparable so the declaration becomes class Program : IComparable.

  4. Activate the smart tag under IComparable. The following two options will appear:

    • Implement interface 'IComparable'.

    • Explicitly implement interface 'IComparable'.

  5. Choose Implement interface 'IComparable'.

IntelliSense will then add a CompareTo method from the IComparable interface to the Program class:

   #region IComparable Members
   public int CompareTo(object obj)
   {
      throw new Exception("The method or operation is not implemented.");
   }
   #endregion

See Also

Reference

Implement Interface

Interfaces (C# Programming Guide)

Explicit Interface Implementation (C# Programming Guide)

Other Resources

Automatic Code Generation