Interface in C #

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 usually 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 example:

The following example 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 (); } } } 

Running the above code we get the following result:

 Transaction: 001 
Transaction date: 08/06/2018
Quantity: 789000
Transaction: 002
Transaction date: 09/06/2018
Quantity: 4519000

According to Tutorialspoint

Previous article: Operator overloading in C #

Next lesson: Namespace in C #

4 ★ | 1 Vote

May be interested

  • Namespace in C #Photo of Namespace in C #
    in c #, namespaces are designed to keep a set of distinct names separated. the same class names that are declared in different namespaces do not conflict with each other
  • Preprocessor directive in C #Photo of Preprocessor directive in C #
    preprocessor directive directives provide instructions to the compiler to preprocess the information before the actual compilation process begins. all preprocessor directives in c # start with #, and only white-space characters can appear before a preprocessor directive in a line. preprocessor directives in c # are not commands, so they do not end with a semicolon (;).
  • Regular Expression in C #Photo of Regular Expression in C #
    a regular expression is a pattern that can be matched to an input text. the .net framework provides a regular expression that allows matching like that. in c #, a pattern consists of one or more character constants, operators, or construct.
  • Handling exceptions (Try / Catch / Finally) in C #Photo of Handling exceptions (Try / Catch / Finally) in C #
    an exception is an issue that occurs during the execution of a program. an exception in c # is a response to an exception situation that occurs while a program is running, such as dividing by zero.
  • I / O file in C #Photo of I / O file in C #
    a file is a collection of data stored on the drive with a specific name and a directory path. when a file is opened for reading or writing, it becomes a stream.
  • Attribute in C #Photo of Attribute in C #
    the attribute in c #, is a declaration tag, used to transmit information to the runtime about the behavior of various elements such as classes, methods, structures, enum, assemblies, etc. in the program. yours. you can add declaration information to the program using attribute.