Now that we've seen how value types and reference types work in C#, let's
An interface can also contain properties, indexers, and events. For more information about these kinds of
Interfaces play an extremely important role in object-oriented design. We can define interfaces to represent
By writing a generic method that takes an interface type as a parameter, and then implementing that interface in
Using interfaces in the design of our systems has several benefits:
It enables us to
It allows us to provide several alternative
It provides a hook for future extensions to our code. We can come back to our application in six months time and define new implementing classes if necessary, without having to change code that relies on them as the interface stays the same.
C++ and Java developers should be familiar with this concept, but this will be new ground for Visual Basic developers.
By convention, all interfaces in the .NET Framework start with the letter I . This makes it easy to distinguish interfaces from classes and structures. The .NET Framework class library has interfaces such as IDisposable , ICloneable , IComparable , and so on.
In this chapter, we've seen how the .NET Framework defines a Common Type System that
All primitive types (except
) map to predefined value types in the .NET Framework class library. For example, if we declare an
variable in our C# code, the compiler maps this to
. This in
Classes represent reference types. When we create a class object, it is placed on the Common Language Runtime's managed heap. Class objects exhibit copy-by-reference semantics. When a class object is no longer referenced, the object becomes available for garbage collection.
Structures represent value types. Value instances can be stored on the heap, or within managed objects. Value instances exhibit copy-
Enumerations are integral data types that have a restricted set of
Delegates represent pointers to
Interfaces specify a
Now that we've seen how to define types in C#, we'll investigate how to design and implement these types correctly and effectively in the following chapters.