1

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.

Airn5475
  • 2,452
  • 29
  • 51
Roberto Rizzi
  • 1,525
  • 5
  • 26
  • 39
  • 1
    Not entirely sure what you are asking here. So, your `switch` works, but you don't like it? – Casey Crookston Sep 13 '19 at 13:21
  • Correct me if I'm wrong (I don't know PHP) but isn't the original code in PHP that you are trying to emulate also just a switch? – Casey Crookston Sep 13 '19 at 13:23
  • https://dotnetfiddle.net/CjpUSD Dictionary lookup is another option, could point to existing methods as well. – Caramiriel Sep 13 '19 at 13:31
  • 1
    C# has [Funcs](https://learn.microsoft.com/en-us/dotnet/api/system.func-1?view=netframework-4.8) and [Actions](https://learn.microsoft.com/en-us/dotnet/api/system.action-1?view=netframework-4.8). You can read about those [here](https://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate). They're often represented via [lambda expressions](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) (similar to the PHP syntax you've shown) those this isn't necessary. – mason Sep 13 '19 at 14:59
  • Thanks for the comments!I will look into lambda albeit not entirely sure it's what I'm looking for. What I'm asking here is if there is a way of having a C# code that behaves like that PHP snippet or at least in a similar way so I can start separating things and give some order. The switch works well despite there may be few flaws but I don't want to get rid of it just for fun.:-) I need to do that as there are too many things tangled up around it, stuff of different domains, methods too big including the method itself where the switch is, hence I need to restructure that spaghetti monolith – Roberto Rizzi Sep 14 '19 at 20:20

0 Answers0