不能转换类型“字符串”到“廉政?”通过引用转换,装箱转换,解包转换,包装转换或null类型转换

StackOverflow https://stackoverflow.com/questions/887943

  •  23-08-2019
  •  | 
  •  

C#接受以下:

object o = "hello";
int? i = o as int?;

if (i == null) {
    // o was not a boxed int
}
else {
    // Can use i.Value to recover the original boxed value
}

但不

String o = "hello";
int? i = o as int?;

if (i == null) {
    // o was not a boxed int
}
else {
    // Can use i.Value to recover the original boxed value
}

我只是想知道关于关键字as的C#中的行为。

在相同的Java这会失败:

Object test1 = "hello";
Integer test2 = (Integer) test1;
String test3 = "hello";
Integer test4 = (Integer) test3; //compilation error
有帮助吗?

解决方案

在编译器知道一个字符串可以永远是int?所以它会告诉你。这并不意味着int?是没有用的。您尝试使用案例远非正常的一个。正常一个是“我要代表一个整数,该值是缺少/未知的可能性”。为此,int?运作非常良好。

为什么你的期望的原密码工作?它为什么会有帮助?

请注意,您的可以的使用as可空类型,拆箱:

object o = "hello";
int? i = o as int?;

if (i == null)
{
    // o was not a boxed int
}
else
{
    // Can use i.Value to recover the original boxed value
}

编辑:在看到你的评论,你不使用as解析的东西。你可能想使用 int.TryParse

string text = "123":
int value;
if (int.TryParse(text, out value))
{
    Console.WriteLine("Parsed successfully: {0}", value);
}
else
{
    Console.WriteLine("Unable to parse text as an integer");
}

如果你确定该字符串意味着是一个整数(即它是一个错误,否则),那么你可以使用的 int.Parse

int value = int.Parse(text);

如果解析失败这将抛出异常。

还请注意,这两种方法允许你指定的格式提供者(通常是一个文化信息),它允许你表达怎样的数字在格式表示(例如千位分隔符)。

编辑:在回答你的新问题,编译器防止这一点,因为它知道一个字符串不能的可能的是装箱的int - 转换将永远不会成功。当只知道原来的值是一个对象,它的可能的成功。

例如,假设我对你说,“这是一个的形状:它是一个正方形”这是一个合理的问题。这是合理的,问它:你无法不看形状告诉

但是,如果我说:“这是一个三角形:它是一个正方形”然后你会合理有权在我的脸上笑,为三角形不可能是一个正方形 - 问题就没有意义了。

其他提示

INT?装置可为空的整数类型,而不是一个int可能含有的任何其它类型的变量。

如果你想有一个变量的类型,可以包含一个int或字符串,你必须使用一个对象,或者一个字符串,我想,然后生活充满型铸造的生活。我不知道你为什么会想这样做,虽然。

INT?允许你存储的任何整数值,或空值。当说,这个问题的答案“有多少订单已经这个人放在”是合法的“我不知道”,而不是数量的订单,或零,这将是“我知道一个事实,这个人从来没有,这是有用下了订单”。

我要添加一些进一步的信息。

这是另一种情况下,为什么铸造无效,编译器会引发对编译的误差,即的 System.String 被标记为密封。所以编译器知道从哪个类型的 System.String inherites和哪些类型的可以使用的的 - 运算符投的字符串。

由于关键字的密封的,编译器也知道你的无法的继承 System.String ,以添加功能或执行一些额外的接口

下面的代码是一个例子,并且编译器将抛出编译以下错误

  

无法类型“SealedClass”转换到   经由参考“ICastToInterface”   转换,装箱转换,   取消装箱转换,包装   转换,或空类型转换

public class UnsealedClass {
  // some code
}

public sealed class SealedClass {
  // some code
}

public interface ICastToInterface {
  // some code
}

public class Test {

  public Test() {
    UnsealedClass unsealedClass = new UnsealedClass();

    SealedClass sealedClass = new SealedClass();

    ICastToInterface unsealedCast = unsealedClass as ICastToInterface;  // This works fine

    ICastToInterface sealedCast = sealedClass as ICastToInterface; // This won´t compile, cause SealedClass is sealed
  }
}

但是可以检查的空值并将其设置为空。

int? blah;
if (blah == null)
{}

INT?是一个空整数,它无关铸件和作为关键字。 “字符串”是字符串类型的对象,这是无法转换为一个int(可为空的或不可为空的)。

AS关键字几乎一样使用,除了它括号不会返回错误铸造,它将设置对象为null:

int i = 1;

object o = i; //boxing

int j = (int)o; //unboxing

此第一示例适用作为分配给对象O是一个int。

现在考虑:

string i = "MyString";

object o = MyString;

 int j = (int)o //unboxing raises exception

 int j = o as int; //raises compilation Error as int is not nullable
 int? j = o as int?; /// o == null

我希望这有助于解释这两个概念之间的差异。

理查德

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top