c# - 这两个 Actor 之间的区别

标签 c# class object interface

我真的不完全熟悉类型转换。因此,请随时编辑或评论我的问题的更改。

假设我有一个实现接口(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/

相关文章:

c++ - 没有匹配函数调用 'class'

Java:类内部的静态方法,用于创建对象

c# - 使用数据中的参数调用 C# 方法

c# - 命名空间 'Transactions' 中不存在类型或命名空间名称 'System'

Java 初学者 GUI 单选按钮

c++ - 临时对象有身份吗?

java - 使用 toString() 方法打印 String Array 对象的编号列表

java - 将 java 代码转换为 c# 代码(十六进制字符串到字节数组)

c# - 为什么在将 double 转换为十进制时会出现 InvalidCastException

c++ - 表达式 : _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error