Click here to Skip to main content
15,895,423 members
Home / Discussions / C#
   

C#

 
AnswerRe: Indexers Pin
DaveyM6925-Aug-12 0:37
professionalDaveyM6925-Aug-12 0:37 
AnswerRe: Indexers Pin
PIEBALDconsult25-Aug-12 4:40
mvePIEBALDconsult25-Aug-12 4:40 
AnswerRe: Indexers Pin
Richard MacCutchan25-Aug-12 7:07
mveRichard MacCutchan25-Aug-12 7:07 
GeneralRe: Indexers Pin
ripples25-Aug-12 8:52
ripples25-Aug-12 8:52 
Questioncan this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
BillWoodruff24-Aug-12 20:31
professionalBillWoodruff24-Aug-12 20:31 
AnswerRe: can this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
DaveyM6925-Aug-12 0:13
professionalDaveyM6925-Aug-12 0:13 
GeneralRe: can this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
BillWoodruff25-Aug-12 11:25
professionalBillWoodruff25-Aug-12 11:25 
AnswerRe: could this be done without dynamic Pin
Martijn Kok25-Aug-12 0:59
Martijn Kok25-Aug-12 0:59 
It can be done with a
C#
List<KeyValuePair<Type, object>>

The example doesn't use the advantages of a dynamic (dynamic binding) over an object (static binding).

At this article http://msdn.microsoft.com/en-us/magazine/gg598922.aspx[^] the advantages are explained. Objects and dynamics are for a large part the same with one exception that the bindings aren't resolved until runtime. It is as if you tell the compiler "trust me, I know what I'm doing" Cool | :cool:

If the dymanic contains a System.Windows.Form.Textbox you could do this
C#
dynamic something = new TextBox();
something.Text = "Hello dynamic world!"; //OK

You wouldn't be able to do this with an object. The programmer has to make sure that the something variable does have a Text property. Otherwise you would get a RuntimeBindingException.
C#
dynamic something = "Change the value to a string";
something.Text = "Hello dynamic world!"; //throws RuntimeBindingException


Another advantage of a dynamic would that you could call a method with a specific signature (because the real type of the dynamic is resolved at runtime).
C#
public void DoSomething(string someValue) { Console.WriteLine("The value is a string: {0}", someValue); }
public void DoSomething(double someValue) {
Console.WriteLine("The value is a double: {0}", somevalue); }

public void CallTheRightDoSomething()
{
  dynamic value = 0.5;
  DoSomething(value); // The value is a double: 0.5

  value = "Now use it as a string";
  DoSomehting(value); // The value is a string: Now use it as a string
}

With objects this would give an error. Due to the static binding nature of objects you would get a designtime error. var could solve this, but not as flexible as dynamic.

I guess using dynamic would only make sense if you really needs the dynamic binding behaviour. If you have functions that have different signatures to handle the values in your list (string, double, textbox), then it would be necessary to use dynamics. Otherwise you could use object.
GeneralRe: could this be done without dynamic Pin
BillWoodruff25-Aug-12 11:33
professionalBillWoodruff25-Aug-12 11:33 
Questionasp.net Pin
Abubakar Shaikh .24-Aug-12 20:05
Abubakar Shaikh .24-Aug-12 20:05 
AnswerRe: asp.net Pin
Richard MacCutchan24-Aug-12 21:57
mveRichard MacCutchan24-Aug-12 21:57 
AnswerRe: asp.net Pin
Abhinav S25-Aug-12 2:38
Abhinav S25-Aug-12 2:38 
AnswerRe: asp.net Pin
Dave Kreskowiak25-Aug-12 4:23
mveDave Kreskowiak25-Aug-12 4:23 
QuestionProblem in datagridviewDeleteColumn Pin
Member 925960624-Aug-12 19:44
Member 925960624-Aug-12 19:44 
QuestionHow To Binding A List Of Frame? Pin
Bardiamarzbani24-Aug-12 9:42
Bardiamarzbani24-Aug-12 9:42 
AnswerRe: How To Binding A List Of Frame? Pin
Wes Aday24-Aug-12 10:46
professionalWes Aday24-Aug-12 10:46 
AnswerRe: How To Binding A List Of Frame? Pin
Dave Kreskowiak25-Aug-12 4:21
mveDave Kreskowiak25-Aug-12 4:21 
QuestionLearning C# for a beginner Pin
Diego Carrion24-Aug-12 8:16
Diego Carrion24-Aug-12 8:16 
AnswerRe: Learning C# for a beginner Pin
Richard Andrew x6424-Aug-12 9:10
professionalRichard Andrew x6424-Aug-12 9:10 
GeneralRe: Learning C# for a beginner Pin
Diego Carrion24-Aug-12 9:16
Diego Carrion24-Aug-12 9:16 
GeneralRe: Learning C# for a beginner Pin
Richard Andrew x6424-Aug-12 9:20
professionalRichard Andrew x6424-Aug-12 9:20 
GeneralRe: Learning C# for a beginner Pin
Diego Carrion24-Aug-12 9:29
Diego Carrion24-Aug-12 9:29 
GeneralRe: Learning C# for a beginner Pin
Richard Andrew x6424-Aug-12 10:07
professionalRichard Andrew x6424-Aug-12 10:07 
AnswerRe: Learning C# for a beginner Pin
wizardzz24-Aug-12 9:51
wizardzz24-Aug-12 9:51 
GeneralRe: Learning C# for a beginner Pin
Diego Carrion24-Aug-12 10:06
Diego Carrion24-Aug-12 10:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.