C# new syntax

asked 2022-06-03T18:33:40.603+00:00
T.Zacks 3,906 Reputation points

see the below code and tell me is it a regular function CalculateToll() whose syntax is now bit different ?

public class TollCalculator  
{  
    public decimal CalculateToll(object vehicle) =>  
        vehicle switch  
    {  
        Car c           => 2.00m,  
        Taxi t          => 3.50m,  
        Bus b           => 5.00m,  
        DeliveryTruck t => 10.00m,  
        { }             => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),  
        null            => throw new ArgumentNullException(nameof(vehicle))  
    };  
}  

2) what is the meaning of this line using toll_calculator;

all code taken from this url https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/pattern-matching

thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,904 questions
No comments
1 vote

Accepted answer
  1. answered 2022-06-03T19:31:51.99+00:00
    Karen Payne MVP 29,146 Reputation points Microsoft MVP

    Using top down style is harder to comprehend code so look at it from old school.

    using System;
    using Calculators;
    using CommercialRegistration;
    using ConsumerVehicleRegistration;
    using LiveryRegistration;
    
    namespace ConsumerVehicleRegistration
    {
        partial class Program
        {
            static void Main(string[] args)
            {
                var tollCalc = new TollCalculator();
                Car car = new ();
                Console.WriteLine(tollCalc.CalculateToll(car));
                Console.ReadLine();
            }
        }
    }
    
    namespace Calculators
    {
        public class TollCalculator
        {
            public decimal CalculateToll(object vehicle) =>
                vehicle switch
                {
                    Car c => 2.00m,
                    Taxi t => 3.50m,
                    Bus b => 5.00m,
                    DeliveryTruck t => 10.00m,
                    { } => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
                    _ => throw new ArgumentNullException(nameof(vehicle))
                };
        }
    }
    namespace ConsumerVehicleRegistration
    {
        public class Car
        {
            public int Passengers { get; set; }
        }
    }
    
    namespace CommercialRegistration
    {
        public class DeliveryTruck
        {
            public int GrossWeightClass { get; set; }
        }
    }
    
    namespace LiveryRegistration
    {
        public class Taxi
        {
            public int Fares { get; set; }
        }
    
        public class Bus
        {
            public int Capacity { get; set; }
            public int Riders { get; set; }
        }
    }
    

1 additional answer

Sort by: Most helpful
  1. answered 2022-06-04T18:37:36.78+00:00
    Bruce (SqlWork.com) 27,846 Reputation points

    1) the pattern match is using the type pattern match. often you want access a property of the type, you you can define an identifier which is types and passed to the match expression

    var x = y switch  
    { 
        Car c => c. Passengers, 
        _ => 0 
    }; 
    

    this is not an assignment, if y is of type Car, return the value of Passengers. that is patten matching used with a switch or an if statements is an expression returning a value, not an assignment.

    but in the sample code the identifier is not used, so a better version is:

      public decimal CalculateToll(object vehicle) => vehicle switch 
         { 
             Car            => 2.00m, 
             Taxi           => 3.50m, 
             Bus            => 5.00m, 
             DeliveryTruck  => 10.00m, 
             { }            => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)), 
             null           => throw new ArgumentNullException(nameof(vehicle)) 
         }; 
    

    2) CalculateToll() is a regular function, written as a single line expression. C# allows

    public bool DoNot1(bool x) 
    { 
        return !x; 
    } 
    
    public bool DoNot2(bool x) => !x; 
    

    both these function work the same;

    3) it a namespace. I believe it is a typo, I think they wanted:

    using Calculators;