空条件运算符(C#6.0)【难度:1级】:
答案1:
public static class CarInformation
{
public static int GetNumberOfGears(this Car car)
{
return car?.Engine?.GearBox?.NumberOfGears ?? -1;
}
}
答案2:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
// ...
return car?.Engine?.GearBox?.NumberOfGears ?? -1;
}
}
答案3:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
return car?.Engine?.GearBox?.NumberOfGears ?? -1;;
}
}
答案4:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
return (car?.Engine?.GearBox?.NumberOfGears)??-1;
}
}
答案5:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
return car.Engine?.GearBox?.NumberOfGears ?? -1;
}
}
答案6:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
// ...
return car.Engine?.GearBox?.NumberOfGears ?? -1;
}
}
答案7:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
return(car.Engine?.GearBox?.NumberOfGears ?? -1);
}
}
答案8:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
return car.Engine?.GearBox?.NumberOfGears ?? -1;
// ...
}
}
答案9:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
return (car.Engine?.GearBox?.NumberOfGears) ?? -1;
}
}
答案10:
public static class CarInformation
{
public static int? GetNumberOfGears (this Car car)
{
return car?.Engine?.GearBox?.NumberOfGears ?? -1;
}
}
答案11:
using System;
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
return car?.Engine?.GearBox?.NumberOfGears ?? -1;
// ...
}
}
答案12:
using System;
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
return car?.Engine?.GearBox?.NumberOfGears ?? -1;
}
}
答案13:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
=> car?.Engine?.GearBox?.NumberOfGears ?? -1;
}
答案14:
public static class CarInformation
{
public static int GetNumberOfGears(this Car c)
{
return c?.Engine?.GearBox?.NumberOfGears ?? -1;
}
}
答案15:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
int? num;
num=car?.Engine?.GearBox?.NumberOfGears;
if (num==null)
{return -1;}
else
{return (int)num;}
}
}
答案16:
using System;
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
int result = -1;
try{
result = car.Engine.GearBox.NumberOfGears;
}catch(Exception ex){
}
return result;
}
}
答案17:
static class CarInformation
{
public static int GetNumberOfGears (this Car car) => car?.Engine?.GearBox?.NumberOfGears ?? -1;
}
答案18:
public static class CarInformation
{
public static int GetNumberOfGears (this Car car)
{
int? numberOfGears = car?.Engine?.GearBox?.NumberOfGears ?? -1;
return numberOfGears.Value;
}
}