I've recently moved to C# from PHP and I'm looking for the something close to a PHP-code like the following:
$i_hate_switches = [
'value1' => function() {
//my operations for getting value 1
return $myValue;
},
'value2' => function() {
//my operations for getting value 2
return $myValue;
}
];
$myValue = $i_hate_switches['value1'];
This question arose since I'm trying to get rid / split up of a quite complex switch in a C# project and I remembered that I used the above "technique" a couple of times in PHP. I believe they're called anonymous functions in PHP but I'm not sure about C#. It very worked well at the time with PHP though.
Of course I'm aware that C# is a different language with generics, more constraints etc, in fact I'm not looking for exactly the same solution but I was wondering if there were something similar, implemented obviously in a different way but getting a similar result.
//-------------------------------------------------------------------------
Just to give you a glimpse of what I'm doing this is the switch I'm trying to split up:
switch (ProductType)
{
case ProductType.Broadband:
return await GetBroadbandDashboardData(subscription.SubscriptionId);
case ProductType.Fixednet:
return await GetLandlineDashboardData(subscription);
case ProductType.Mobile:
case ProductType.MobileBroadband:
return await GetMobileDashboardDataByVersion(request, subscription, encryptedSubscriptionId);
case ProductType.Tv:
return GetTvDashboardData(subscription, request);
default:
return null;
or another case is
var dashboardItems = new List<IDashboardItem>();
if (technicalStack == TechnicalStack.MBilling)
//dashboardItems.AddRange(mBillingItems);
}
else if (technicalStack == TechnicalStack.Another)
{
//dashboardItems.AddRange(anotherItems);
}
//...
These ifs / switches are now in controllers and I'd like to split the code up so that I can reorganize them into relevant adapters. Unfortunately, I didn't choose the original structure and what has been done so far! :-)
Of course I'm also open to any other suggestion or documentation that might help me with this.