PrepAway - Latest Free Exam Questions & Answers

Which two code segments can you use to achieve this goal?

You are developing an application that includes a method named SendMessage.
You need to ensure that the SendMessage() method is called with the required parameters.
Which two code segments can you use to achieve this goal? (Each correct answer presents a
complete solution. Choose two.)

PrepAway - Latest Free Exam Questions & Answers

A.
Option A

B.
Option B

C.
Option C

D.
Option D

Explanation:
D: ExpandoObject
Represents an object whose members can be dynamically added and removed at run time.
/ The ExpandoObject class enables you to add and delete members of its instances at run time and
also to set and get values of these members. This class supports dynamic binding, which enables you
to use standard syntax like sampleObject.sampleMember instead of more complex syntax like
sampleObject.GetAttribute(“sampleMember”).
/ You can pass instances of the ExpandoObject class as parameters. Note that these instances are
treated as dynamic objects in C# and late-bound objects in Visual Basic. This means that you do not
have IntelliSense for object members and you do not receive compiler errors when you call nonexistent members. If you call a member that does not exist, an exception occurs.
Note:
* Visual C# 2010 introduces a new type, dynamic. The type is a static type, but an object of type
dynamic bypasses static type checking. In most cases, it functions like it has type object. At compile
time, an element that is typed as dynamic is assumed to support any operation. Therefore, you do
not have to be concerned about whether the object gets its value from a COM API, from a dynamic
language such as IronPython, from the HTML Document Object Model (DOM), from reflection, or
from somewhere else in the program. However, if the code is not valid, errors are caught at run
time.

4 Comments on “Which two code segments can you use to achieve this goal?

  1. Lord Vader says:

    var is static typed – the compiler and runtime know the type – they just save you some typing… the following are 100% identical:

    var s = “abc”;
    Console.WriteLine(s.Length);
    and

    string s = “abc”;
    Console.WriteLine(s.Length);
    All that happened was that the compiler figured out that s must be a string (from the initializer). In both cases, it knows (in the IL) that s.Length means the (instance) string.Length property.

    dynamic is a very different beast; it is most similar to object, but with dynamic dispatch:

    dynamic s = “abc”;
    Console.WriteLine(s.Length);
    Here, s is typed as dynamic. It doesn’t know about string.Length, because it doesn’t know anything about s at compile time. For example, the following would compile (but not run) too:

    dynamic s = “abc”;
    Console.WriteLine(s.FlibbleBananaSnowball);
    At runtime (only), it would check for the FlibbleBananaSnowball property – fail to find it, and explode in a shower of sparks.

    With dynamic, properties / methods / operators / etc are resolved at runtime, based on the actual object. Very handy for talking to COM (which can have runtime-only properties), the DLR, or other dynamic systems, like javascript.




    0



    0

Leave a Reply