Table of Contents
Interface in C # is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
The interface in C # is defined as a syntactical contract that all Interface inheritance classes should follow. The interface defines the " What " section of the contract and the derived classes define the " How " section of the contract.
Interface defines attributes, methods and events, which are members of that Interface. The Interface contains only declarations of these members. Defining members is the responsibility of the derived class. It often helps provide a standard Structure that derived classes should follow.
The abstract classes to some extent typically serve the same purpose, however, they are mainly used when there are only a few methods declared by the base class and the derived class implements function.
Declaring Interface in C #
To declare the Interface in C # we use the interface keyword. It is similar to class declaration. By default, the Interface command is public. This is the Interface declaration example in C #:
public interface GiaoDich { //các thành viên của interface void xemGiaoDich (); double laySoLuong (); }
For instance,:
the example below will illustrate the Interface implementation above:
using System . Collections . Generic ; using System . Linq ; using System . Text ; using System ; namespace GiaoDienUngDung { public interface QTM GiaoDich { //các thành viên của interface void xemGiaoDich (); double laySoLuong (); } public class GiaoDich : QTMGiaoDich { private string ma_giao_dich ; private string ngay_giao_dich ; private double so_luong ; //Lớp GiaoDich kế thừa từ QTMGiaoDich public GiaoDich () { ma_giao_dich = " " ; ngay_giao_dich = " " ; so_luong = 0.0 ; } public GiaoDich ( string c , string d , double a ) { ma_giao_dich = c ; ngay_giao_dich = d ; so_luong = a ; } public double laySoLuong () { return so_luong ; } public void xemGiaoDich () { Console . WriteLine ( "Giao dịch: {0}" , ma_giao_dich ); Console . WriteLine ( "Ngày giao dịch: {0}" , ngay_giao_dich ); Console . WriteLine ( "Số lượng: {0}" , laySoLuong ()); } } class Tester { static void Main ( string [] args ) { GiaoDich so 1 = new GiaoDich ( "001" , "08/06/2018" , 789000.00 ); GiaoDich so2 = new GiaoDich ( "002" , "09/06/2018" , 4519000.00 ); so1 . xemGiaoDich (); so2 . xemGiaoDich (); Console . ReadKey (); } } }
Next lesson: Namespace in C #
Frequently Asked Questions
What should you know about declaring Interface in C #?
To declare the Interface in C # we use the interface keyword. It is similar to class declaration. By default, the Interface command is public. This is the Interface declaration example in C #:
What is Interface in C #?
The interface in C # is defined as a syntactical contract that all Interface inheritance classes should follow.
Why is Interface in C # important?
A clear understanding of Interface in C # helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
Was this article helpful?
Your feedback helps us improve.
Reader Comments 0
Sign in with email or Google to join the discussion.