天天看点

C#练习题答案: 空条件运算符(C#6.0)【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战答案1:答案2:答案3:答案4:答案5:答案6:答案7:答案8:答案9:答案10:答案11:答案12:答案13:答案14:答案15:答案16:答案17:答案18:

空条件运算符(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;
  }
}
​

           

继续阅读