Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
GeneralRe: [c#]Place objects in tablelayoutpanel cells in specific order Pin
Richard MacCutchan17-Oct-14 2:15
mveRichard MacCutchan17-Oct-14 2:15 
GeneralRe: [c#]Place objects in tablelayoutpanel cells in specific order Pin
BillWoodruff17-Oct-14 2:45
professionalBillWoodruff17-Oct-14 2:45 
GeneralRe: [c#]Place objects in tablelayoutpanel cells in specific order Pin
Richard MacCutchan17-Oct-14 2:56
mveRichard MacCutchan17-Oct-14 2:56 
AnswerRe: [c#]Place objects in tablelayoutpanel cells in specific order Pin
BillWoodruff17-Oct-14 0:25
professionalBillWoodruff17-Oct-14 0:25 
QuestionPlay/Pause/Stop media through ASIO driver using C# Pin
Praveen Raghuvanshi16-Oct-14 18:16
professionalPraveen Raghuvanshi16-Oct-14 18:16 
AnswerRe: Play/Pause/Stop media through ASIO driver using C# Pin
Brisingr Aerowing17-Oct-14 10:08
professionalBrisingr Aerowing17-Oct-14 10:08 
GeneralRe: Play/Pause/Stop media through ASIO driver using C# Pin
Praveen Raghuvanshi26-Oct-14 7:37
professionalPraveen Raghuvanshi26-Oct-14 7:37 
Questionanother Func-enstein weirdness : and yet another bad example of MS documentation PinPopular
BillWoodruff16-Oct-14 11:38
professionalBillWoodruff16-Oct-14 11:38 
I was reading Marc Clifton's "Succinctly" book, "Imperative to Functional Programming" (published by SyncFusion, free download here: [^]), when I came across this interesting C# example on page #14:
C#
static int EvenOrZero(int x)
{
  return x % 2 == 0 ? x : 0;
}

int[] numbers = { 1, 2, 3, 4, 5 };

int sumOfEvenNumbers = numbers.Sum((Func<int, int>)EvenOrZero);
Of course, Marc's code works. What intrigued me was the cast of the Function to Func<int,int>> about which Marc comments: "In this example, the cast is necessary to resolve the ambiguity between Func<int, int> and Func<int, int?>"

Since the 'Sum method has an overload which use only int, and not int?: Enumerable.Sum<TSource>> Method (IEnumerable<TSource>, Func<TSource, Int32>), I thought I could do something like this to avoid the cast:
C#
private int EvenOrZero(int x)
{
    return x % 2 == 0 ? x : 0;
}

List<int> numbers = new List<int>{ 1, 2, 3, 4, 5 };

int sumOfEvenNumbers = numbers.Sum<int>(EvenOrZero);
Intellisense flags this with "Ambiguous invocation," and compiling this produces 3 errors:
Error	3	Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)

Error	4	The call is ambiguous between the following methods or properties: 'System.Linq.Enumerable.Sum<int>(System.Collections.Generic.IEnumerable<int>, System.Func<int,decimal>)' and 'System.Linq.Enumerable.Sum<int>(System.Collections.Generic.IEnumerable<int>, System.Func<int,decimal?>)'

Error	5	'int ...EvenOrZero(int)' has the wrong return type
Looking up the MS Doc for this overload of 'Sum [^] leads to an example that does not use int, but uses double !

What gives here with the required cast ? I assume that somehow the direct use of a named method in this case does not satisfy the compiler, but I have used named methods as the value of functions before with no problem.
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali


modified 16-Oct-14 18:09pm.

AnswerRe: another Func-enstein weirdness : and yet another bad example of MS documentation Pin
Gerry Schmitz16-Oct-14 17:43
mveGerry Schmitz16-Oct-14 17:43 
AnswerRe: another Func-enstein weirdness : and yet another bad example of MS documentation Pin
OriginalGriff16-Oct-14 19:27
mveOriginalGriff16-Oct-14 19:27 
AnswerRe: another Func-enstein weirdness : and yet another bad example of MS documentation Pin
Richard Deeming17-Oct-14 5:28
mveRichard Deeming17-Oct-14 5:28 
Questionnecessity to cast return value to nullable in lambda expression ? Pin
BillWoodruff16-Oct-14 7:25
professionalBillWoodruff16-Oct-14 7:25 
AnswerRe: necessity to cast return value to nullable in lambda expression ? Pin
Richard Deeming16-Oct-14 8:12
mveRichard Deeming16-Oct-14 8:12 
GeneralRe: necessity to cast return value to nullable in lambda expression ? Pin
BillWoodruff16-Oct-14 8:44
professionalBillWoodruff16-Oct-14 8:44 
GeneralRe: necessity to cast return value to nullable in lambda expression ? Pin
Richard Deeming16-Oct-14 8:49
mveRichard Deeming16-Oct-14 8:49 
AnswerRe: necessity to cast return value to nullable in lambda expression ? Pin
Pete O'Hanlon16-Oct-14 8:13
mvePete O'Hanlon16-Oct-14 8:13 
GeneralRe: necessity to cast return value to nullable in lambda expression ? Pin
BillWoodruff16-Oct-14 8:44
professionalBillWoodruff16-Oct-14 8:44 
QuestionHow to read tables in Excel Worksheet witc C# and XML Pin
Member 1110881716-Oct-14 3:13
Member 1110881716-Oct-14 3:13 
AnswerRe: How to read tables in Excel Worksheet witc C# and XML Pin
BillWoodruff16-Oct-14 5:25
professionalBillWoodruff16-Oct-14 5:25 
GeneralRe: How to read tables in Excel Worksheet witc C# and XML Pin
Member 1110881716-Oct-14 21:14
Member 1110881716-Oct-14 21:14 
AnswerRe: How to read tables in Excel Worksheet witc C# and XML Pin
Eddy Vluggen16-Oct-14 9:05
professionalEddy Vluggen16-Oct-14 9:05 
QuestionC#/C wrapper Pin
Nikolaj jensen16-Oct-14 2:39
Nikolaj jensen16-Oct-14 2:39 
AnswerRe: C#/C wrapper Pin
Richard MacCutchan16-Oct-14 2:59
mveRichard MacCutchan16-Oct-14 2:59 
GeneralRe: C#/C wrapper Pin
Nikolaj jensen16-Oct-14 3:52
Nikolaj jensen16-Oct-14 3:52 
GeneralRe: C#/C wrapper Pin
Richard MacCutchan16-Oct-14 3:56
mveRichard MacCutchan16-Oct-14 3:56 

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.