Switch Case is one of the most traditional way of having conditional break up performed. Here is the new way which directs, delegates and performs the action as needed.
Ideally I have seen developer opting for If Else statements, adding my source code were I need to set a check bit based on the parameter Range.
int mynumbercheck=1000;
//Your number to be checked
var myswitch = new Dictionary <Func<int,bool>, Action>
{
{ x => x < 10 , () => //Do this!... },
{ x => x < 100 , () => //Do this!... },
{ x => x < 1000 , () => //Do this!... },
{ x => x < 10000 , () => //Do this!... } ,
{ x => x < 100000 , () => //Do this!... },
{ x => x < 1000000 , () => //Do this!... }
};
Now to call our conditional switch
myswitch.First(sw => sw.Key(mynumbercheck)).Value();
Hope this Help you and reduces if else code block
Excellent!
ReplyDeleteThanks, this resolved the problem with the range of numbers without "If" o "Switch" sentences :)
Thanks - elegant
ReplyDeleteThis is indeed an elegant solution...I used a Console.WriteLine just to see if it worked. I'd like to use the found value as a variable, but the Action delegate doesn't return the value. How can I do this?
ReplyDeletepublic static void Main()
Delete{
var mynumbercheck = 40;
var myswitch = new Dictionary , Action>()
{
{ x => x < 8.33, () => Console.WriteLine("1") },
{ x => x < 16.66, () => Console.WriteLine("2")},
{ x => x < 25.00, () => Console.WriteLine("3")},
{ x => x < 33.33, () => Console.WriteLine("4")},
{ x => x < 41.66, () => Console.WriteLine("5")},
{ x => x < 50.00, () => Console.WriteLine("6")},
{ x => x < 58.33, () => Console.WriteLine("7")},
{ x => x < 66.66, () => Console.WriteLine("8")},
{ x => x < 75.00, () => Console.WriteLine("9")},
{ x => x < 83.33, () => Console.WriteLine("10")}
};
myswitch.First(sw => sw.Key(mynumbercheck)).Value();
}
Hi Dino,
DeleteHere is the updated code for you..
var mynumbercheck = 40;
//updated from by Akshaya
var myswitch = new Dictionary, Action>
{
{ x => x < 8.33, () => {Console.WriteLine("1");} },
{ x => x < 16.66, () => {Console.WriteLine("2");}},
{ x => x < 25.00, () => {Console.WriteLine("3");}},
{ x => x < 33.33, () => {Console.WriteLine("4");}},
{ x => x < 41.66, () => {Console.WriteLine("5");}},
{ x => x < 50.00, () => {Console.WriteLine("6");}},
{ x => x < 58.33, () => {Console.WriteLine("7");}},
{ x => x < 66.66, () => {Console.WriteLine("8");}},
{ x => x < 75.00, () => {Console.WriteLine("9");}},
{ x => x < 83.33, () => {Console.WriteLine("10");}}
};
myswitch.First(sw => sw.Key(Convert.ToInt32(mynumbercheck))).Value();
Console.ReadLine();
There seems to be a Typo
DeleteThe Action operator should look like this
--New Dictionary < Func < int,bool>, Action>
This is cool, i tried it and it work, but i just a beginner, can you explian me about those keyword combination that make up var myswitch..
ReplyDeleteThank you..
Glad to know things worked out for you.
Deletevariables are assigned based on the Action and Delegate (how Switch-Case Works)
For more please ref SDK's
Thanks Akshaya
Wow! Dhamaka for sure!
ReplyDeleteSo beautiful, must to have in your toolbox
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWhile this may work sometimes, even most of the time, iterating over the items in a Dictionary is not guaranteed to be the same order they were added.
ReplyDeleteFrom https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.7.2
For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.
There is no reason to use a Dictionary, to get consistent behavior, a List of KeyValuePairs could be used instead and order will be maintained.