PrepAway - Latest Free Exam Questions & Answers

Which two code segments should you use?

You are developing an application that includes the following code segment:

You need to implement both Start() methods in a derived class named UseStart that uses the Start()
method of each interface.
Which two code segments should you use? (Each correct answer presents part of the solution.
Choose two.)

PrepAway - Latest Free Exam Questions & Answers

A.
Option A

B.
Option B

C.
Option C

D.
Option D

E.
Option E

F.
Option F

Explanation:
B:
* Implementing Multiple Interfaces
A class can implement multiple interfaces using the following syntax:
C#

public class CDAndDVDComboPlayer : ICDPlayer, IDVDPlayer
If a class implements more than one interface where there is ambiguity in the names of members, it
is resolved using the full qualifier for the property or method name. In other words, the derived class
can resolve the conflict by using the fully qualified name for the method to indicate to which
interface it belongs
* In C#, both inheritance and interface implementation are defined by the : operator, equivalent to
extends and implements in Java. The base class should always be leftmost in the class declaration.

9 Comments on “Which two code segments should you use?

  1. sv1slim says:

    C & D:

    Implementing multiple interfaces can sometimes result in a collision between member signatures. You can resolve such collisions by explicitly implementing an interface member. Consider the following example:

    interface I1 { void Foo(); }
    interface I2 { int Foo(); }

    public class Widget : I1, I2
    {
    public void Foo()
    {
    Console.WriteLine (“Widget’s implementation of I1.Foo”);
    }
    int I2.Foo()
    {
    Console.WriteLine (“Widget’s implementation of I2.Foo”);
    return 42;
    }
    }
    Because both I1 and I2 have conflicting Foo signatures, Widget explicitly implements I2’s Foo method. This lets the two methods coexist in one class. The only way to call an explicitly implemented member is to cast to its interface:

    Widget w = new Widget();
    w.Foo(); // Widget’s implementation of I1.Foo
    ((I1)w).Foo(); // Widget’s implementation of I1.Foo
    ((I2)w).Foo(); // Widget’s implementation of I2.Foo

    Another reason to explicitly implement interface members is to hide members that
    are highly specialized and distracting to a type’s normal use case. For example, a
    type that implements ISerializable would typically want to avoid flaunting its
    ISerializable members unless explicitly cast to that interface.




    3



    0

Leave a Reply