INTERFACE-ENDINTERFACE
|
WNSupported in Synergy .NET on Windows
|
|
|
[PUBLIC] [PARTIAL] INTERFACE name[<T[(constraints)], ...>] & [EXTENDS base_interface, ...] member_def . . . ENDINTERFACE
Arguments
PUBLIC
(optional) Access is not restricted.
PARTIAL
(optional) The interface definition can be separated into two or more definitions that will be compiled together as a single definition.
name
The name of the interface to declare.
T
A generic type parameter, which indicates the interface is a generic type and which acts as a placeholder for which an actual type will be substituted when the member is used. Note that you can use any unused letter (not just T). See Generic types (.NET) for more information.
constraints
One or more of the following constraints:
- A base class, to stipulate that the generic type parameter must inherit from that specific base class. If no base class is specified, the default is System.Object.
- One or more interfaces, to stipulate that the generic type parameter must implement the specified interfaces.
- A constructor, to allow creating an instance of the type parameter using the NEW syntax.
Multiple constraints must be separated by commas. See Constraints for more information.
EXTENDS base_interface
(optional) The interface will extend the specified base interface and will thus inherit members from that base interface.
member_def
The declaration of a method prototype, property or indexer prototype, or event. You can specify as many member definitions as you want, although you must specify at least one.
Discussion
Methods, properties, or indexer accessors that have an implementation cannot be specified as members. By default, members within an interface have PUBLIC accessibility and are VIRTUAL and non-STATIC. However, on .NET 6 and higher, you can explicitly declare interface members as STATIC ABSTRACT, and any implementing classes must implement those static methods and properties. (See Examples below.)
When an interface extends a base interface, it inherits all members from the base interface as well as all members the base interface inherits from its parent, in recursive fashion.
Using the PARTIAL modifier makes it possible to split the definition of an interface over two or more source files.
In projects that target .NET Core or .NET 5 and higher, interface methods and properties can have default implementations, making them similar to abstract base classes but with the added possibility of multiple inheritance. A class that implements an interface containing default implementations does not have access to those methods and properties defined for the interface unless that class instance is cast to a variable that has the exact type of the interface.
In .NET Core or .NET 5 and higher projects, interface methods and properties support the following modifiers: PUBLIC, PRIVATE, PROTECTED, INTERNAL, SEALED, ABSTRACT, and VIRTUAL.
See also
- IMPLEMENTS interface in the CLASS statement
- @interface data type
public interface iface3 extends iface1, iface2 method func1, i4 endmethod method func2, void p1, i4 endmethod endinterface
The example below illustrates the basic functionality of default interface methods in .NET Core or .NET 5 and higher. Since Car provides implementations of both Drive() and AddGasoline(), its methods will be used in favor of the default implementation defined in IVehicle. Since Airplane doesn’t implement Drive(), the implementation on the interface will be chosen when an Airplane instance is used through a variable with the type IVehicle. An Airplane instance held in an Airplane variable has no access to Drive().
public interface IVehicle method Drive, void proc Console.WriteLine("vehicle is driving") endmethod method AddGasoline, void endmethod endinterface public class Car implements IVehicle public method AddGasoline, void proc Console.WriteLine("adding gasoline") endmethod public method Drive, void proc Console.WriteLine("car is driving") endmethod endclass public class Airplane implements IVehicle public method AddGasoline, void proc Console.WriteLine("adding jet fuel") endmethod endclass
The following example shows how to declare a static abstract method or property within an interface (available in .NET 6 and higher).
namespace ns1 public interface iface1 static abstract method smeth1, void endmethod static abstract readwrite property sprop1, int static abstract property sprop2, int method get endmethod method set endmethod endproperty endinterface endnamespace
And then implement this interface in a class:
namespace ns1 public class class1 implements iface1 public static method smeth1, void proc console.writeline("in class1.smeth1") end public static readwrite property sprop1, int private static sfld1, int public static property sprop2, int method get proc mreturn sfld1 end method set proc sfld1 = value end endproperty endclass endnamespace
And call these abstract methods using a type parameter that is constrained to that interface type:
namespace ns1 static class App public static method testit<T(iface1)>, void proc T.smeth1() T.sprop1 = 8 Console.WriteLine(T.sprop1) T.sprop2 = 9 Console.WriteLine(T.sprop2) endmethod endclass endnamespace main proc ns1.App.testit<class1>(); end