Click here to Skip to main content
15,121,624 members
Home / Discussions / C#
   

C#

 
GeneralRe: Question About Async Pin
Richard Deeming30-Sep-21 6:05
mveRichard Deeming30-Sep-21 6:05 
GeneralRe: Question About Async Pin
Kevin Marois30-Sep-21 6:20
professionalKevin Marois30-Sep-21 6:20 
AnswerRe: Question About Async Pin
Richard Deeming29-Sep-21 22:54
mveRichard Deeming29-Sep-21 22:54 
GeneralRe: Question About Async Pin
Kevin Marois30-Sep-21 5:23
professionalKevin Marois30-Sep-21 5:23 
QuestionMakeGenericType/CreateInstance Object to underlying Type at run-time: why this works is driving me crazy Pin
BillWoodruff28-Sep-21 6:45
mveBillWoodruff28-Sep-21 6:45 
AnswerRe: MakeGenericType/CreateInstance Object to underlying Type at run-time: why this works is driving me crazy Pin
Richard Deeming28-Sep-21 7:20
mveRichard Deeming28-Sep-21 7:20 
GeneralRe: MakeGenericType/CreateInstance Object to underlying Type at run-time: why this works is driving me crazy Pin
BillWoodruff28-Sep-21 10:47
mveBillWoodruff28-Sep-21 10:47 
AnswerRe: MakeGenericType/CreateInstance Object to underlying Type at run-time: why this works is driving me crazy Pin
OriginalGriff28-Sep-21 7:30
mveOriginalGriff28-Sep-21 7:30 
Dynamic types skip all compile time checking in favour of runtime checking - that includes method names, return types, number of parameters, type of parameters, or even that the method exists at all.

So because the return value is declared as dynamic, the compiler doesn't care what you try to call - and at run time the checks are done and the right method is called.
Try it:
C#
dynamic items = new List<string>();
items.Add("Hello");
items.Add("World");
Console.WriteLine(string.Join(" ", items));
items.FallOverAndDie();
Will give you:
Hello World
Unhandled exception. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Collections.Generic.List<string>' does not contain a definition for 'FallOverAndDie'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1[T0](CallSite site, T0 arg0)
   at Program.Main(String[] args)
Because the collection does not contain a FallOverAndDie method.
C#
dynamic items = new List<string>();
items.Add("Hello");
items.Add("World");
Console.WriteLine(string.Join(" ", items));
items = new Dictionary<int, int>();
items.Add("Hello");
items.Add("World");
Console.WriteLine(string.Join(" ", items));
Will fail because the Dictionary class has no overload with just one parameter.

Personally, I'm still not convinced that adding dynamic to the language was a good idea - it seems too much like a way to move compile time errors to runtime and break strong typing ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!

GeneralRe: MakeGenericType/CreateInstance Object to underlying Type at run-time: why this works is driving me crazy Pin
BillWoodruff28-Sep-21 11:19
mveBillWoodruff28-Sep-21 11:19 
GeneralRe: MakeGenericType/CreateInstance Object to underlying Type at run-time: why this works is driving me crazy Pin
OriginalGriff28-Sep-21 11:40
mveOriginalGriff28-Sep-21 11:40 
GeneralRe: MakeGenericType/CreateInstance Object to underlying Type at run-time: why this works is driving me crazy Pin
BillWoodruff29-Sep-21 1:57
mveBillWoodruff29-Sep-21 1:57 
Questionled matrix display Pin
Member 1537173026-Sep-21 8:49
MemberMember 1537173026-Sep-21 8:49 
AnswerRe: led matrix display Pin
OriginalGriff26-Sep-21 9:33
mveOriginalGriff26-Sep-21 9:33 
QuestionC# 8 nullable reference question / conundrum Pin
Super Lloyd25-Sep-21 2:36
MemberSuper Lloyd25-Sep-21 2:36 
QuestionConvering time representation format Pin
Alex Dunlop22-Sep-21 0:26
MemberAlex Dunlop22-Sep-21 0:26 
AnswerRe: Convering time representation format Pin
Peter_in_278022-Sep-21 2:28
professionalPeter_in_278022-Sep-21 2:28 
AnswerRe: Convering time representation format PinPopular
Richard Deeming22-Sep-21 2:37
mveRichard Deeming22-Sep-21 2:37 
AnswerRe: Convering time representation format Pin
BillWoodruff22-Sep-21 23:00
mveBillWoodruff22-Sep-21 23:00 
AnswerRe: Convering time representation format Pin
Pete O'Hanlon23-Sep-21 3:03
mvePete O'Hanlon23-Sep-21 3:03 
GeneralRe: Convering time representation format Pin
Alex Dunlop23-Sep-21 10:22
MemberAlex Dunlop23-Sep-21 10:22 
QuestionRe: Convering time representation format Pin
Member 1532961323-Sep-21 10:32
MemberMember 1532961323-Sep-21 10:32 
GeneralRe: Convering time representation format Pin
Mycroft Holmes23-Sep-21 13:50
professionalMycroft Holmes23-Sep-21 13:50 
GeneralRe: Convering time representation format Pin
Pete O'Hanlon23-Sep-21 21:29
mvePete O'Hanlon23-Sep-21 21:29 
GeneralRe: Convering time representation format Pin
Peter_in_278023-Sep-21 22:03
professionalPeter_in_278023-Sep-21 22:03 
QuestionHow to convert Persian date representation? Pin
Alex Dunlop15-Sep-21 21:31
MemberAlex Dunlop15-Sep-21 21:31 

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.