C# 如何将null转换为默认值(浮点)或0

C# 如何将null转换为默认值(浮点)或0,c#,C#,我试图获取浮点值作为输入,如果跳过一个值,将其设置为0,但由于跳过将其设置为null,我会得到一个异常。所以,我试着用??像这样的操作员: float speed = Convert.ToSingle(Console.ReadLine()) ?? default(float); 或 但我有一个错误: 错误CS0019:运算符“??”不能应用于类型为的操作数 “float”和“int” 我该如何写这个等价物:float y=x??0; 您需要使用0f来告诉编译器您的0是浮点而不是int 有关详细

我试图获取浮点值作为输入,如果跳过一个值,将其设置为0,但由于跳过将其设置为null,我会得到一个异常。所以,我试着用??像这样的操作员:

float speed = Convert.ToSingle(Console.ReadLine()) ?? default(float);

但我有一个错误:

错误CS0019:运算符“??”不能应用于类型为的操作数 “float”和“int”

我该如何写这个等价物:float y=x??0;

您需要使用0f来告诉编译器您的0是浮点而不是int

有关详细信息,请参阅。

您不能。 因为Convert.ToSingle返回float而不是float?结果永远不会为null。

Convert.ToSingle将引发异常。如果格式未知,则它永远不会返回null。您应该使用Single.TryParse轻松检查结果是否正确

float speed;
if (!Single.TryParse(Console.ReadLine(), out speed))
    speed = 0f;

还请注意,这些方法将使用当前区域设置,因此它们可能在不同的区域设置上接受不同的输入。如果某个区域设置(例如十进制分隔符)需要输入,请确保指定正确的区域设置。除可为null的引用类型外,不能将null合并运算符与值类型一起使用。但是,您可以使用此一行:

float speed =  Single.TryParse(Console.ReadLine(), out speed) ? speed : default(float);

已经发布了有效的答案,但我想应该给出一些背景信息,说明为什么这个错误CS0019:Operator???不能应用于类型为“float”和“int”的操作数

在C语言中,我们有引用类型和值类型。区别在于

引用类型的变量存储对其数据对象的引用,而值类型的变量直接包含其数据

如中所述


因为float、int和type实际上是值类型,所以很明显为什么不能与这些类型一起使用。它们不能为null,因为它们存储的不是任何可能为null的引用,而是值本身。

回答您的问题:

我该如何写这个等价物:float y=x??0


成功了。谢谢各位。这是一个对我有效的解决方案,以防有人好奇

public static void CalculateDistanceTimeSpeed()
{
    float speed;
    float distance;
    float time;

    Console.WriteLine("Find distance/speed/time based on any of the two.");
    Console.WriteLine("Enter two of the three requirements");
    Console.WriteLine();

    Console.WriteLine("Enter speed in km/h");
    if (!Single.TryParse(Console.ReadLine(), out speed))
        speed = 0f;

    Console.WriteLine("Enter distance in km");
    if (!Single.TryParse(Console.ReadLine(), out distance))
        distance = 0f;

    Console.WriteLine("Enter time in hour/s");
    if (!Single.TryParse(Console.ReadLine(), out time))
        time = 0f;

    if (speed == 0f)
    {
        speed = distance / time;
        Console.WriteLine("Required average Speed : {1}km/h for distance {0}km and time {2}hrs.", distance, speed.ToString("0.##"), time);
    }

    else if (distance == 0f)
    {
        distance = speed * time;
        Console.WriteLine("Distance traveled : {0}km at speed {1}km/h for {2}hrs.", distance.ToString("0.##"), speed, time);

    }

    else
    {
        time = distance / speed;
        Console.WriteLine("Travel time: {2}hrs with speed {1}km/h for distance {0}km.", distance, speed, time.ToString("0.##"));
    } 

    // add mph conversion
}

我在这里没有看到空值。使用float.TryParse.First search为什么会出现此错误?因为数据类型不可为空。因此,您可以检查如何使其可为null。但他不能对值类型使用null合并运算符,只能对可以为null引用类型的类型使用null合并运算符。他的问题是,我将如何编写此等价项:float y=x??0;事实并非如此,您可以将null合并运算符用于可为null的类型,这些类型也是值类型。
float speed =  Single.TryParse(Console.ReadLine(), out speed) ? speed : default(float);
float? x = 10f;
float y = x ?? 0f;
public static void CalculateDistanceTimeSpeed()
{
    float speed;
    float distance;
    float time;

    Console.WriteLine("Find distance/speed/time based on any of the two.");
    Console.WriteLine("Enter two of the three requirements");
    Console.WriteLine();

    Console.WriteLine("Enter speed in km/h");
    if (!Single.TryParse(Console.ReadLine(), out speed))
        speed = 0f;

    Console.WriteLine("Enter distance in km");
    if (!Single.TryParse(Console.ReadLine(), out distance))
        distance = 0f;

    Console.WriteLine("Enter time in hour/s");
    if (!Single.TryParse(Console.ReadLine(), out time))
        time = 0f;

    if (speed == 0f)
    {
        speed = distance / time;
        Console.WriteLine("Required average Speed : {1}km/h for distance {0}km and time {2}hrs.", distance, speed.ToString("0.##"), time);
    }

    else if (distance == 0f)
    {
        distance = speed * time;
        Console.WriteLine("Distance traveled : {0}km at speed {1}km/h for {2}hrs.", distance.ToString("0.##"), speed, time);

    }

    else
    {
        time = distance / speed;
        Console.WriteLine("Travel time: {2}hrs with speed {1}km/h for distance {0}km.", distance, speed, time.ToString("0.##"));
    } 

    // add mph conversion
}