我真的不完全熟悉类型转换。因此,请随时编辑或评论我的问题的更改。
假设我有一个实现接口(interface)的类:
public class Class1: Interface1
{
}
这两者有什么区别?
Interface1 myObject = new Class1();
和
Class1 myClassObject = new Class1();
Interface1 myObject = (Interface1) myClassObject;
第一个也是一种类型转换形式吗?
编辑:
每个人做什么?
最佳答案
第一个确实是隐式转换。引用微软:
For reference types, an implicit conversion always exists from a class to any one of its direct or indirect base classes or interfaces. No special syntax is necessary because a derived class always contains all the members of a base class.
第二个强制转换是一个显式转换,正如已经提到的,由于上述原因没有必要。当一些信息可能/将会在转换过程中丢失并告诉编译器如何处理时,显式转换是必要的。
Microsoft 有一篇关于转换的好文章:http://msdn.microsoft.com/en-us/library/ms173105.aspx
关于c# - 这两个 Actor 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15289848/