Embedded interfaces - Object-oriented programming

Embedded interfaces

Interfaces allow you to describe some desirable properties and methods that different classes must have. In the FCL library, there are a large number of standard interfaces. For example, all classes that allow comparison of their objects usually inherit the interface IComparable, the implementation of which allows you to compare objects only for equality, but also for equality on & quot; more & quot; or & quot; less than & quot;.

There are many interfaces described in the FCL library. They are used by different library classes as well as classes created by developers.

An example of such an interface is the interface IComparable, which has only one CompareTo method (object obj), which returns an integer value, positive, negative or zero, depending on the & quot; more & quot; ; less than & quot; or & quot; is equal to & quot;.

Typically, the class is first defined by the CompareTo method, and then the overloaded operations are introduced to perform a comparison of objects in the usual way using the operation relationship characters. To enter the order relation on the Person class discussed earlier, you need to make this class the heir to the interface IComparable. To do this, you must implement the interface CompareTo method in this class:

public class Person: IComparable {public int CompareTo (object obj) {

Person p = prs as Point; if (! p.Equals (null))

return (string.Compare (fam, p.fam)); else

throw new ArgumentException (& quot; bad type & quot;);

}

// other class components

}

Since the method parameter must have a universal object type, it must be cast to the Person type before performing the comparison. This cast is performed using the as operation, which checks the correctness of the cast types.

The order relation on objects of the class Person is given as the order relation on the people's surnames. Since the strings inherit the interface IComparable, the string.Compare (fam, p.fam) method is called for the names of people, and the result is returned as the result of the CompareTo method for comparing people. If the method parameter does not match the type you are looking for, an exception is thrown with a special notification.

The introduced methods can be checked using the following test case: public void TestCompare () {

Person ps1 = new Regsop ("Ivanovs");

Person ps2 = new Person ('TleTpoB & quot;);

Person ps3 = new Regsop (& quot; Sidorov);

Console.WriteLine ("{0} & gt; {1} = {2}", psI.Fam, ps2.Fam, (ps1 & gt; ps2));

Console.WriteLine (& quot; {0} & lt; {1} = {2} & quot ;, ps2.Fam, ps3.Fam, (ps2 & lt; ps3));

}

Result:

Ivanov & gt; Petrov = False Petrov & lt; Sidors = True

Other interfaces that are actively used in FCL class classes are lEnumerable, ICollection, and IList, which describe non-generic collections of objects that can be invoked by an index. The main methods and properties of the ICollection and IList interfaces are described in Table. 6.1 and 6.2.

Table 6.1

Basic elements of the ICollection interface

Element

Description

Count

property - the number of objects in the collection

Soruto ()

the method of copying the elements of the collection into an array of type Array, beginning with the specified index of the array

GetEnumerator ()

getting an object that supports the lEnumerable interface), which allows you to view the entire collection (for foreach)

Table 6.2

Basic elements of the IList interface

Element

Description

Add ()

method of adding an object to the collection

Clear ()

Deleting all objects from the collection

Contains ()

check for presence in the collection of the object (true false)

lndexOf ()

index definition for a given object

lnsert ()

inserting an object into a place specified by an index

Remove ()

removing the first encountered instance of the specified object

RemoveAt ()

Deleting all objects starting at the specified index

[index]

An indexer that allows you to get or set objects at a given index

These interfaces are supported by different properties of the FCL library classes. For example, the Controls property in the Form class (for developing a graphical interface) or the Tables property in the DateSet class (for organizing a disconnected database mode).

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)