IMyIF_A
and
IMyIF_B
. Thus, when
MyClass
implements both of these interfaces, it explicitly
implements each one separately, fully qualifying its name in the process. Since the only way
that an explicitly implemented method can be called is on an interface reference,
MyClass
creates two such references, one for
IMyIF_A
and one for
IMyIF_B
. It then calls two of its
own methods, which call the interface methods, thereby removing the ambiguity.
Choosing Between an Interface and an Abstract Class
One of the more challenging parts of C# programming is knowing when to create an interface
and when to use an abstract class in cases in which you want to describe functionality but
not implementation. The general rule is this: When you can fully describe the concept in
terms of “what it does” without needing to specify any “how it does it,” then you should
use an interface. If you need to include some implementation details, then you will need to
represent your concept in an abstract class.
The .NET Standard Interfaces
The .NET Framework defines a large number of interfaces that a C# program can use. For
example,
System.IComparable
defines the
CompareTo( )
method, which allows objects to be
compared when an ordering relationship is required. Interfaces also form an important part
of the Collections classes, which provide various types of storage (such as stacks and queues)
for groups of objects. For example,
System.Collections.ICollection
defines the functionality
of a collection.
System.Collections.IEnumerator
offers a way to sequence through the
elements in a collection. These and many other interfaces are described in Part II.
Structures
As you know, classes are reference types. This means that class objects are accessed through
a reference. This differs from the value types, which are accessed directly. However, sometimes
it would be useful to be able to access an object directly, in the way that value types are. One
reason for this is efficiency. Accessing class objects through a reference adds overhead onto
every access. It also consumes space. For very small objects, this extra space might be
significant. To address these concerns, C# offers the structure. A
structure
is similar to a
class, but is a value type, rather than a reference type.
Structures are declared using the keyword
struct
and are syntactically similar to classes.
Here is the general form of a
struct
:
www.freepdf-books.com
Do'stlaringiz bilan baham: |