Click here to Skip to main content
15,888,337 members
Home / Discussions / C#
   

C#

 
AnswerRe: hello Pin
Ravi Bhavnani28-Aug-12 5:26
professionalRavi Bhavnani28-Aug-12 5:26 
Questionhow to store datas in excel. Pin
Anusha Sridhar28-Aug-12 0:09
Anusha Sridhar28-Aug-12 0:09 
AnswerRe: how to store datas in excel. PinPopular
Eddy Vluggen28-Aug-12 0:21
professionalEddy Vluggen28-Aug-12 0:21 
SuggestionRe: how to store datas in excel. Pin
pramod.hegde28-Aug-12 2:02
professionalpramod.hegde28-Aug-12 2:02 
AnswerRe: how to store datas in excel. Pin
PIEBALDconsult28-Aug-12 7:06
mvePIEBALDconsult28-Aug-12 7:06 
QuestionParsing a Path Pin
ASPnoob27-Aug-12 19:09
ASPnoob27-Aug-12 19:09 
AnswerRe: Parsing a Path Pin
Pete O'Hanlon27-Aug-12 23:27
mvePete O'Hanlon27-Aug-12 23:27 
Questionoh yes, using 'dynamic <i>will<i> do something special for you ! Pin
BillWoodruff27-Aug-12 15:33
professionalBillWoodruff27-Aug-12 15:33 
Hi, this thread represents a summary of what I have learned since starting the thread [^] on this forum: re use of dynamic to allow direct access to a 'value without the need for casting that 'value (currently an 'object) to its appropriate type.

In the code below I compare and contrast for three different types of .NET types, KeyValuePairs, Tuples, and List<dynamic> what using 'dynamic enables compared to using 'object in the constructor of these types.

I am particularly interested in the reactions of Martijn Kok and DaveyM69, who were kind enough to post interesting comments on the original thread: I believe, that what I demonstrate here does change the implications of one comment by Martijn Kok (as I understood that comment: and my understanding may well be in error).

Of course, comments would be welcome about use of 'dynamic in general, and the potential "overhead" of using a Type like List<dynamic> !

I would also be interested in knowing your opinion if there is enough material here for a "Tip/Trick."

thanks, Bill
XML
// KeyValuePair declarations
private KeyValuePair<Type, object> Kvp1 = new KeyValuePair<Type, object> (typeof(int), 199);
private KeyValuePair<Type, dynamic> Kvp2 = new KeyValuePair<Type, dynamic>(typeof(int), 199);

// Tuple declarations
private Tuple&lt;Type, object&gt; twoTuple1 = new Tuple&lt;Type, object&gt;(typeof(int), 199);
private Tuple&lt;Type, dynamic&gt; twoTuple2 = new Tuple&lt;Type, dynamic&gt;(typeof(int), 199);

// List<dynamic> declaration
private List&lt;dynamic&gt; dyList = new List&lt;dynamic&gt;();

// method that demonstrates use of declared objects above
private void TestDynamicVsNonDynamic()
{
    // using KeyValuePair

    //int a = Kvp1.Value + 1;      // will not compile
    int b = Kvp2.Value + 1;        // will compile, give expected result

    // using Tuple

    //int x = twoTuple1.Item2 + 1; // will not compile
    int y = twoTuple2.Item2 + 1;   // will compile, give expected result

    // using List<dynamic>

    dyList.Add(199);                // will compile    

    dyList.Add(new TextBox{Text = "some text&"});

    int z = dyList[0] + 1;          // will compile, give expected result

    dyList[1].Text += " ... blah, blah blah";

    string s = dyList[1].Text;      // will compile, give expected result

    Type dyList1Type = dyList[1].GetType(); // returns expected Type: TextBox

}


"If you shoot at mimes, should you use a silencer ?" Stephen Wright

AnswerRe: oh yes, using 'dynamic will do something special for you ! Pin
DaveyM6927-Aug-12 22:34
professionalDaveyM6927-Aug-12 22:34 
GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
BillWoodruff28-Aug-12 3:34
professionalBillWoodruff28-Aug-12 3:34 
GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
DaveyM6928-Aug-12 3:50
professionalDaveyM6928-Aug-12 3:50 
GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
BillWoodruff28-Aug-12 14:58
professionalBillWoodruff28-Aug-12 14:58 
AnswerRe: oh yes, using 'dynamic will do something special for you ! Pin
DaveyM6928-Aug-12 4:15
professionalDaveyM6928-Aug-12 4:15 
GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
BillWoodruff28-Aug-12 15:09
professionalBillWoodruff28-Aug-12 15:09 
GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
DaveyM6928-Aug-12 21:24
professionalDaveyM6928-Aug-12 21:24 
QuestionMailMessage syntax with using statement Pin
thewazz27-Aug-12 10:46
professionalthewazz27-Aug-12 10:46 
AnswerRe: MailMessage syntax with using statement Pin
Eddy Vluggen27-Aug-12 11:01
professionalEddy Vluggen27-Aug-12 11:01 
AnswerRe: MailMessage syntax with using statement Pin
Wes Aday27-Aug-12 11:03
professionalWes Aday27-Aug-12 11:03 
GeneralRe: MailMessage syntax with using statement Pin
thewazz27-Aug-12 12:32
professionalthewazz27-Aug-12 12:32 
QuestionPlay Incoming Message Notification Pin
Jassim Rahma27-Aug-12 10:43
Jassim Rahma27-Aug-12 10:43 
AnswerRe: Play Incoming Message Notification Pin
Wes Aday27-Aug-12 10:45
professionalWes Aday27-Aug-12 10:45 
GeneralRe: Play Incoming Message Notification Pin
Jassim Rahma27-Aug-12 10:52
Jassim Rahma27-Aug-12 10:52 
GeneralRe: Play Incoming Message Notification Pin
Wes Aday27-Aug-12 11:00
professionalWes Aday27-Aug-12 11:00 
AnswerRe: Play Incoming Message Notification Pin
Eddy Vluggen27-Aug-12 11:08
professionalEddy Vluggen27-Aug-12 11:08 
GeneralRe: Play Incoming Message Notification Pin
Wes Aday27-Aug-12 11:15
professionalWes Aday27-Aug-12 11:15 

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.