如何将int转换为可空类型?

Cod*_*eel 2 c#

我只是在做简单的int swap程序.我也刚刚学会了铸造,所以我在这个程序中尝试了.但我收到一个错误,说我应该做一个明确的演员.

我不认为我理解演员的概念.有什么问题?

static void Main(string[] args)
{
    Console.WriteLine("Enter the FIRST Integer.");
    string tempint1 = Console.ReadLine();
    int int1 = Convert.ToInt32(tempint1);

    Console.WriteLine("Enter the SECOND Integer.");
    string tempint2 = Console.ReadLine();
    int int2 = Convert.ToInt32(tempint2);

    Console.WriteLine();

    int? swapInt = null;
    swapInt = (int)int1;
    int1 = int2;
    int2 = swapInt;

    Console.WriteLine("The integers have been swapped!");
    Console.WriteLine("The FIRST INTEGER is now " + int1);
    Console.WriteLine("The SECOND INTEGER is now " + int2);
    Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)

The*_*kis 6

int?是一种叫做的类型的简写Nullable<int>.显然,然后,intNullable<int>不是同一类型.Nullable<int>有一个名为的属性Value,该属性是实际的int.要访问该Value属性,首先应检查该HasValue属性是否正确true(就像检查引用类型是否一样null).

这是问题,现在:

有一个隐式转换intNullable<int>(记住,它是相同的int?)并且转换表示Nullable<int>创建了一个new ,HasValue设置为trueand并将其Value设置为初始值int.因此,您可以这样做:

int a = 5
int? b = 5; // implicit conversion
int? c = a; // implicit conversion
int? d = c;
Run Code Online (Sandbox Code Playgroud)

另一方面,相反的转换,Nullable<int>从而int不存在隐式,因为Nullable<int>可能将其HasValue属性设置为false,这意味着常规int将无法描述它并且编译器不知道你是什么想要在这种情况下发生.所以,这就是为什么你做不到的原因:

int? a = 5;
int b = a; // illegal, no implicit conversion exists
Run Code Online (Sandbox Code Playgroud)

相反,你想要做这样的事情:

int? a = 5;
if(a.HasValue)
{
    int b = a.Value;
    // ...
}
else
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

强制转换与问题无关(这只是编译器的建议,但实际上并不是你想要做的).为了学习:强制转换只是指令编译器将一种类型的表达式视为另一种(希望是兼容的)类型的表达式,可能是通过在这两种类型之间应用转换(如果在任何地方定义了这种转换) ).


Chr*_*tos 5

swapInt 是一个可以为空的int及以下:

你试图将一个可空int分配给一个问题的int:

int2 = swapInt;
Run Code Online (Sandbox Code Playgroud)

如果你把它改成以下的那就没问题,

int2 = swapInt.Value;
Run Code Online (Sandbox Code Playgroud)

此外,这段代码:

int? swapInt = null;
swapInt = (int)int1;
int1 = int2;
int2 = swapInt;
Run Code Online (Sandbox Code Playgroud)

它可以改为以下一个:

 int? swapInt = int1;
 int1 = int2;
 int2 = swapInt.Value;
Run Code Online (Sandbox Code Playgroud)

您不必将已经整数强制转换为int, (int)int1;

更新

我必须对你的程序做一些评论,你可以将它们带到帐户中.

static void Main(string[] args)
{
    // Get the users input.

    Console.WriteLine("Enter the FIRST Integer.");
    string a = Console.ReadLine();

    Console.WriteLine("Enter the SECOND Integer.");
    string b = Console.ReadLine();

    // Variables, in which we will store the parsed strings that user passes.
    int integerA;
    int integerB;

    // Try to parse the strings that user passes to create two integer numbers.
    // If either parse of a or parse of b fail, then the method Int32.TryParse()
    // will return false and the code in the body of if, will not be executed.
    if(Int32.TryParse(a, out integerA) && Int32.TryParse(b, out integerB))
    {
        // Pass the integers to the methods called swap
        Swap(ref integerA, ref integerB);
        Console.WriteLine("The integers have been swapped!");
        Console.WriteLine("The FIRST INTEGER is now " + int1);
        Console.WriteLine("The SECOND INTEGER is now " + int2);
    }

    Console.WriteLine();
}

// This methods makes the actual swap
static void Swap(ref int a, ref int b)
{
    int temp = a;
    a = b;
    b = temp;
}
Run Code Online (Sandbox Code Playgroud)

如果你仔细阅读上面的代码,那么你会注意到我们没有使用nullables,实际上,在这种情况下我们不需要它们.

为了您的信息, C#中有一个通用方法,可用于交换对象,无论它们是否为数字.此方法的名称是Swap,它的签名如下:

static void Swap<T>(ref T lhs, ref T rhs)
Run Code Online (Sandbox Code Playgroud)

您可以使用以下方法:

Swap<int>(ref integerA, ref integerB);
Run Code Online (Sandbox Code Playgroud)

有关此方法的更多信息,请查看此处.

什么是ref代表?

如下所述MSDN:

ref关键字导致参数通过引用传递,而不是通过值传递.通过引用传递的效果是方法中对参数的任何更改都会反映在调用方法中的基础参数变量中.引用参数的值始终与基础参数变量的值相同.

示例让我们有一个名为的方法Increment:

public void Increment(int x)
{
    // This will increase the value of variable x by one.
    // This change is only visible insied the body of this method
    // and it is not reflected outside of it.
    x++;
}
Run Code Online (Sandbox Code Playgroud)

如果我们定义了如下方法:

public void Increment(ref int x)
{
    x++;
}
Run Code Online (Sandbox Code Playgroud)

x所谓的方法体外也可以看到值的变化Increment.

为什么这样?

因为在第一种情况下,我们传递存储在其中的值的副本x,而在第二种情况下,我们不传递此值的副本.因此,第二种情况的任何变化也将反映在被称为方法的主体之外Incerement.