as 語法你用的沒錯, 這個無法轉換 ,所以他報錯 System.Windows.Forms.Form b=a as System.Windows.Forms.Control;
楼上的试都不试也就算了,帮助也不看的?至于LZ的问题,则更是……哎…… System.Windows.Forms.Form a=new System.Windows .Forms .Form(); System.Windows.Forms.Form b=a as System.Windows.Forms.Control;两个都是Form类型的,你在这里转换啥?再说as是用来向下转换而不是向上转换的。 System.Windows.Forms.Control a=new System.Windows .Forms .Form(); System.Windows.Forms.Form b=a as System.Windows.Forms.Form;
c#中怎么會沒有啊.我晕 public static void Main() { B class_b=new B(); A class_a=new A(); A class_a2= class_b as A; Console.WriteLine(class_a2.a); } 这个例子中 A是基类 B是派生类,这个转换就可以的
to: Ivony不好意思,他的程序错误我没具体看,我只是说它的 as 语法没错误 呵呵
用于在兼容的引用类型之间执行转换。例如:string s = someObject as string; 复制代码 if (s != null) { // someObject is a string. } 备注 as 运算符类似于强制转换,所不同的是,当转换失败时,运算符将产生空,而不是引发异常。更严格地说,这种形式的表达式 复制代码 expression as type 等效于 复制代码 expression is type ? (type)expression : (type)null 只是 expression 只被计算一次。注意,as 运算符只执行引用转换和装箱转换。as 运算符无法执行其他转换,如用户定义的转换,这类转换应使用 cast 表达式来执行。示例 复制代码 // cs_keyword_as.cs // The as operator. using System; class Class1 { }class Class2 { }class MainClass { static void Main() { object[] objArray = new object[6]; objArray[0] = new Class1(); objArray[1] = new Class2(); objArray[2] = "hello"; objArray[3] = 123; objArray[4] = 123.4; objArray[5] = null; for (int i = 0; i < objArray.Length; ++i) { string s = objArray[i] as string; Console.Write("{0}:", i); if (s != null) { Console.WriteLine("'" + s + "'"); } else { Console.WriteLine("not a string"); } } } } 输出
0:not a string 1:not a string 2:'hello' 3:not a string 4:not a string 5:not a string C# 语言规范 有关更多信息,请参见 C# 语言规范中的以下各章节:6 转换7.9.10 as 运算符
to windstore(雪海飘香(花落人亡兩不知)) 不好意思,我指楼上的楼上以及楼上的楼上的楼上,看发表时间……太巧了……
应是如下: System.Windows.Forms.Form b=a as System.Windows.Forms.Form; if (a!=null) { //转换成功! }
這個無法轉換 ,所以他報錯
System.Windows.Forms.Form b=a as System.Windows.Forms.Control;
System.Windows.Forms.Form b=a as System.Windows.Forms.Control;两个都是Form类型的,你在这里转换啥?再说as是用来向下转换而不是向上转换的。 System.Windows.Forms.Control a=new System.Windows .Forms .Form();
System.Windows.Forms.Form b=a as System.Windows.Forms.Form;
{
B class_b=new B();
A class_a=new A();
A class_a2= class_b as A;
Console.WriteLine(class_a2.a);
}
这个例子中 A是基类 B是派生类,这个转换就可以的
if (s != null)
{
// someObject is a string.
}
备注
as 运算符类似于强制转换,所不同的是,当转换失败时,运算符将产生空,而不是引发异常。更严格地说,这种形式的表达式 复制代码
expression as type
等效于 复制代码
expression is type ? (type)expression : (type)null
只是 expression 只被计算一次。注意,as 运算符只执行引用转换和装箱转换。as 运算符无法执行其他转换,如用户定义的转换,这类转换应使用 cast 表达式来执行。示例
复制代码
// cs_keyword_as.cs
// The as operator.
using System;
class Class1
{
}class Class2
{
}class MainClass
{
static void Main()
{
object[] objArray = new object[6];
objArray[0] = new Class1();
objArray[1] = new Class2();
objArray[2] = "hello";
objArray[3] = 123;
objArray[4] = 123.4;
objArray[5] = null; for (int i = 0; i < objArray.Length; ++i)
{
string s = objArray[i] as string;
Console.Write("{0}:", i);
if (s != null)
{
Console.WriteLine("'" + s + "'");
}
else
{
Console.WriteLine("not a string");
}
}
}
}
输出
0:not a string
1:not a string
2:'hello'
3:not a string
4:not a string
5:not a string
C# 语言规范
有关更多信息,请参见 C# 语言规范中的以下各章节:6 转换7.9.10 as 运算符
System.Windows.Forms.Form b=a as System.Windows.Forms.Form;
if (a!=null)
{
//转换成功!
}
谢谢,ivony的解释很正确呀,别的兄弟门说的也很有帮助呀.
兄弟我是初学者,还请大家对帮忙呀.