实现接口阵列的隐式类型 [英] Implicit typing of arrays that implement interfaces

查看:145
本文介绍了实现接口阵列的隐式类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印象是,C#编译器将隐类型的数组基于关闭,他们都可以隐式转换为一个类型。



编译器生成
没有最好的类型发现隐式类型数组

 公共接口ISomething {} 

酒店的公共接口ISomething2 {}

公共接口ISomething3 {}

公共类Foo:ISomething {}
公共类酒吧:ISomething,ISomething2 { }
公共类车:ISomething,ISomething3 {}

无效的主要()
{
变种OBJ1 =新的Foo();
变种OBJ2 =新的酒吧();
变种obj3 =新车();

无功对象=新的[] {OBJ1,OBJ2,obj3};
}



我知道,纠正这个方法是声明的类型,如:

    p> 

但后这里的井盖类型的帮助下我。


解决方案

C#编译器认为该类型集合所有指定的元素。它的的考虑公共基类等。



您的可能的投表达式之一:

  VAR对象=新的[] {OBJ1,OBJ2,(ISomething)obj3}; 



...但我个人只希望用明确的方式:

  VAR对象=新ISomething [] {OBJ1,OBJ2,obj3}; 



另外,如果你明确声明的任何或全部 OBJ1 OBJ2 obj3 类型 ISomething , 。这将很好地工作太不改变数组初始化表达式



从C#3规范,部分7.5.10.4:





第三种形式的数组创建表达式被称为
隐式类型的数组创建
表达
的。它类似于
第二形式中,除了将阵列的元件
型未明确
给出,但确定为最好
常见的类型(§7.4.2.13) 。的集数组初始化
表达式




第7.4.2.13这个样子的:




在一些情况下,共同的类型需要
被推断为一组表达式。
在特别是,
的元素类型隐式类型的阵列和返回
型的带
块体的匿名功能以这种方式被发现。
直观上看,给定一组
表达E1 ... EM这一推断
应相当于调用

  TR M< X>(X X 1 ... X XM)

与荣作为参数。更多
精确,推理开出
与不固定类型变量X
的输出类型,然后推断是由
每荣类型X.最后,X
是固定的,所得到的S型是
将所得常见类型为
表达式



I was under the impression that the C# compiler will implicitly type an array based off a type that they can all be implicitly converted to.

The compiler generates No best type found for implicitly-typed array

public interface ISomething {}

public interface ISomething2 {}

public interface ISomething3 {}

public class Foo : ISomething { }
public class Bar : ISomething, ISomething2 { }
public class Car : ISomething, ISomething3 { }

void Main()
{
    var obj1 = new Foo();
    var obj2 = new Bar();
    var obj3 = new Car();

    var objects= new [] { obj1, obj2, obj3 };
}

I know that the way to correct this is to declare the type like:

new ISomething [] { obj1, ...} 

But I'm after an under the covers type help here.

解决方案

The C# compiler considers the set of types of all the specified elements. It does not consider common base types etc.

You could cast one of the expressions:

var objects= new [] { obj1, obj2, (ISomething) obj3 };

... but personally I'd just use the explicit form:

var objects= new ISomething[] { obj1, obj2, obj3 };

Alternatively, if you explicitly declared any or all of obj1, obj2 and obj3 as type ISomething, that would work fine too without changing the array initialization expression.

From the C# 3 spec, section 7.5.10.4:

An array creation expression of the third form is referred to as an implicitly typed array creation expression. It is similar to the second form, except that the element type of the array is not explicitly given, but determined as the best common type (§7.4.2.13) of the set of expressions in the array initializer.

Section 7.4.2.13 looks like this:

In some cases, a common type needs to be inferred for a set of expressions. In particular, the element types of implicitly typed arrays and the return types of anonymous functions with block bodies are found in this way. Intuitively, given a set of expressions E1…Em this inference should be equivalent to calling a method

Tr M<X>(X x1 … X xm)

with the Ei as arguments. More precisely, the inference starts out with an unfixed type variable X. Output type inferences are then made from each Ei with type X. Finally, X is fixed and the resulting type S is the resulting common type for the expressions.

这篇关于实现接口阵列的隐式类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆