• 1 Post
  • 20 Comments
Joined 1 year ago
cake
Cake day: June 12th, 2023

help-circle






  • A switch to per minute, per megabyte plan made me a lot more concious about spending money on my phone. If I want something to watch/listen to during a trip, I download it beforehand. I almost never use any minutes, only communicating via the mobile data. With autodownloading of pictures disabled in all my chat apps, it runs about 50 MB per month, which charges me less than 50 cents.


  • You’d also want your transmitter to be physically on equal distance from the speakers. You always get a bit of current via the electromagnetic field, and it spreads throughout anything, not just wires.

    Well, it also depends on whether that little current is enough to make a sound that the runner would be able to hear.










  • I don’t believe the first code sample is a valid C# code. Switch-case statements expect constants for case conditions and runtime types are not constants, obviously. What you can do is something like this:

    void MyMethod(object arg)
    {
        if (arg.GetType() == typeof(int))
            MyTypedMethod((int)arg);
        else if (arg.GetType() == typeof(int?))
            MyTypedMethod((int?)arg);
        else if (arg.GetType() == typeof(long))
            MyTypedMethod((long)arg);
    }
    

    This will work, but the problem here is that inheritance will fuck you up. All the types we check in this example are sealed and we don’t really have to worry about it in this case, but generally this code is… just bad.

    And yes, the correct way of implementing something like this is by using generic types. Starting from C# 7.0 we have this cool structure:

    void MyMethod<T>(T arg)
    {
        switch (arg)
        {
            case null:
                // Do something with null
                break;
            case int intArg:
                // Do something with intArg
                break;
            case long longArg:
                // Do something with longArg
                break;
        }
    }
    

    You’ll have to check for null separately, as you can’t use Nullable<> there, but overall, it looks much cleaner and handles inheritance properly for you.

    This is called pattern matching, you can read more about it here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns