Reputation: 81342
If I have a dynamic method in C# 4. Can it be used to return for example in 1 call - A string, in another call a Boolean, and in another call an int?
Or is the return type of the dynamic method set after the first runtime call? Meaning if the first time I call the method it returns a boolean, must all subsequent calls to that method also return boolean?
Upvotes: 3
Views: 233
Reputation: 5255
Any object can be converted to dynamic type implicitly, so you should be able to to do so. Dynamic in most cases functions like type object
.
Upvotes: 1
Reputation: 755209
A dynamic
method is free to change it's return data at any point it chooses. For example
class Example {
private int m_count;
public dynamic GetData() {
switch(m_count++) {
case 0: return 42;
case 1: return "hello world";
default: return new object();
}
}
}
A dynamic
typed method is little different than a method that has an object
return type. It's free to return any values which are compatible with object
. The only issue is ensuring the caller of the method can handle the various values.
Upvotes: 8