ENTER REFERENCE TYPE DECLARATIONS
■ The Class1.cs code appears in the parent window.
Á Delete the comments within the Main method.
‡ Type the code that establishes the interface, the fields, the constructor, and then implements the get method in the property implementation.
° Type the code that implements the set method in the property
implementation and the class that outputs the variable.
To save keystrokes, you can implement an interface directly from a class.
CONTINUED
interface Int1: IntBase1, IntBase2 {
void Method1();void Method2();
}
interface Int2: IntBase1, IntBase2 {
void Method3(); void Method4(); void Method5();
}
class Class1: Int1, Int2 {
public static void Main() {
Console.WriteLine("This class inherits from two interfaces that inherit from two base interfaces. No values are returned because all the interfaces do are return void methods.");
}
RESULT:
This class inherits from two interfaces that inherit from two base interfaces. No values are returned because all the interfaces return void methods.
· Run the program by pressing the F5 key.
■ The constant expression appears onscreen.
‚ Save the program as the filename.
— Close the Interface project.
± Click the New Project button in the Start menu.
■ The New Project window appears.
¡ Click the Console Application icon in the Templates pane.
™ Type a name for the file.
£ Click OK.
T
he delegate reference type serves two functions. First, a delegate object serves as the primary object in an event. An event tells your project about something that happens to an object in your program. Second, the delegate object contains method information that tells the affected object in the event what to do when the event occurs.Delegates act like function pointers in other languages such as C++ and Pascal. Unlike other languages, Visual C#
delegates are completely object-oriented so they are secure and type-safe. Type-safe code is code that accesses types in well-defined ways so as to prevent crashing programs that
can lead to other nasty things such as memory leaks and crashing operating systems.
When you create a delegate, you must enter two mandatory options. First, you must enter the result type that matches the return type of the method. Entering the result type lets you tie in the delegate with the method.
Second, you must enter the delegate name. Without either of those options, the MDE window calls your attention to the error. You can add attributes and modifiers as you can with classes and interfaces.
ENTER REFERENCE TYPE DECLARATIONS
ENTER REFERENCE TYPE DECLARATIONS (CONTINUED)
■ The Class1.cs code appears in the parent window.
¢ Delete the comments within the Main method.
∞ Type the code that establishes the delegate, calls the delegate, and outputs the result.
§ Run the program by pressing the F5 key.
■ The constant expression appears onscreen.
¶ Save the program as the filename.
No matter if you write your delegate before or after you write your method, avoid compilation errors by ensuring that the delegate result type and your method return type match before you compile your project.
The greatest similarity between delegates and interfaces is that they separate the specification of methods with the implementation of those methods. As with the class and struct, your decision about using a delegate or an interface depends on what you are trying to do.
If you need to call a single method or you want a class to refer to several methods, use the delegate.
The delegate also has the added advantage of being easier to construct than the interface. However, the interface lets you specify the methods that an object in your project calls instead of general methods that a delegate includes. The interface is also a good choice if a class needs an inheriting interface as a jump point for accessing other interfaces or classes.
Properties
⁄ Click Start ➪ Programs ➪ Microsoft Visual Studio .NET 7.0 ➪ Microsoft Visual Studio .NET 7.0.
¤ Click New Project. ■ The New Project window appears.
‹ Click the Console Application icon in the Templates pane.
› Type a name for the file.
ˇ Click OK.
Console Applicatio
V
isual C# enables you to convert value types to reference types and vice versa with a process called boxing. Boxing refers to the value type to reference type conversion process. Unboxing is the reverse procedure that converts reference types to value types.Visual C# boxes value types, including struct and built-in value types, by copying the value from the value type into the object. After you box the value type, you can change the value of that value type. Boxing is useful when you need to copy a value from one value type to one or more value types. For example, you can copy an integer value to one or
more integers by having those other integers reference the object you created when you boxed the integer value.
Unboxing lets you convert an object into a value type or an interface type into a value type that implements that interface. When Visual C# unboxes the object, it checks the object to see if it is the same value type as the one you specify in the unboxing argument. If Visual C# sees that this is true, it unboxes the object value and places it into the value type.