An analysis of the importance of interfaces in C #

Source: Internet
Author: User

There is the concept of interfaces in C #. I think the interface is like an abstract base class in C + +.
An interface can be understood as a convention that makes the class or structure that implements the interface consistent in form.

declaring interfaces :
Syntax and declaration abstract classes are identical.
  

public interface IBankAccount{    void PayIn(decimal amount);    bool Withdraw(decimal amount); decimal Balance { get; }}

Note: Interfaces can only contain declarations of methods, properties, indexers, and events. It is not allowed to declare modifiers on members, even PUBILC, because interface members are always public and cannot be declared as virtual and static. If a modifier is required, it is best to have the implementation class declare it.

Interface using:

Class saveraccount:ibankaccount{PrivateDecimal balance;PublicDecimal Balance {get {return balance; } }public void PayIn (decimal amount) {balance + = amount;} public bool Withdraw (decimal amount) {if (balance >= amount) {balance-= amount; return true;} Console.WriteLine ( "withdraw failed."); return FALSE;} public override string tostring () {return String.Format ( 

Summary:
1. The interfaces in C # are defined independently of the class. This is in contrast to the C + + model, where interfaces are actually abstract base classes in C + +.
2, interfaces and classes can inherit multiple interfaces.
3. A class can inherit a base class, and an interface cannot inherit a class at all. This model avoids the multiple inheritance problem of C + +, and the implementation of different base classes in C + + may conflict. Therefore, complex mechanisms such as virtual inheritance and explicit scopes are no longer needed. The simplified interface model of C # helps accelerate application development.
4. An interface defines a reference type that has only abstract members. What an interface in C # actually does, there is only a method flag, but there is no execution code at all. This implies that an interface cannot be instantiated, and only one object derived from that interface can be instantiated.
5. Interfaces can define methods, properties, and indexes. So, in contrast to a class, the particularity of an interface is that when a class is defined, it can be derived from a multiple interface, and you can only derive from one of the only classes.
As the interface in C #, as we mentioned at the beginning, is like an abstract class in C + +, but there are also abstract classes in C #, what is the difference between abstract classes and interfaces in C #? See the blog "Analysis of the differences between abstract classes and interfaces in C #"

An analysis of the importance of interfaces in C #

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.