C # Tutorial C # interface (Interface)

Source: Internet
Author: User

C # interface (Interface)

The interface defines the syntax contracts that should be followed when all classes inherit an interface. The interface defines the "what" part of the syntax contract, and the derived class defines the "How to" section of the syntax contract.

An interface defines properties, methods, and events, which are members of an interface. The interface contains only the declarations of the members. The definition of a member is the responsibility of the derived class. The interface provides the standard structure that a derived class should follow.

Abstract classes are somewhat similar to interfaces, but most of them are only used when only a few methods are declared by the base class by a derived class.

declaring interfaces

An interface uses the interface keyword declaration, which is similar to a class declaration. The interface declaration is public by default. The following is an instance of an interface declaration:

public interface itransactions{   //interface member   void Showtransaction ();   Double Getamount ();}

Instance

The following example shows the implementation of the above interface:

Using system.collections.generic;using system.linq;using system.text;namespace interfaceapplication{Public      Interface Itransactions {//interface member void Showtransaction ();   Double Getamount ();      } public class Transaction:itransactions {private string tcode;      private string date;      private double amount;         Public Transaction () {tcode = "";         Date = "";      Amount = 0.0;         } public Transaction (string C, String d, double a) {tcode = C;         date = D;      Amount = A;      } public double Getamount () {return amount;         } public void Showtransaction () {Console.WriteLine ("Transaction: {0}", Tcode);         Console.WriteLine ("Date: {0}", date);      Console.WriteLine ("Amount: {0}", Getamount ()); }} class Tester {static void Main (string[] args) {Transaction T1 = new Transaction ("001", "8/10         /2012 ", 78900.00); Transaction t2 = New Transaction ("002", "9/10/2012", 451900.00);         T1.showtransaction ();         T2.showtransaction ();      Console.readkey (); }   }}

When the above code is compiled and executed, it produces the following results:

transaction:001date:8/10/2012amount:78900transaction:002date:9/10/2012amount:451900

The above is the "C # Tutorial" C # Interface (Interface) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.