简体   繁体   English

当存在显式引用类型时,C# 可以推断 Object 构造上的通用参数吗?

[英]Can C# Infer the Generic Parameter on Object Construction when there's an Explicit Reference Type?

Java can infer generic type parameters when the reference type is explicitly given. Java 可以在显式给出引用类型时推断泛型类型参数。 For example, both of these are fine:例如,这两个都很好:

Set<String> set = new HashSet<string>();
Set<String> set2 = new HashSet<>();

Unfortunately if I try to do something similar in C# it doesn't seem supported:不幸的是,如果我尝试在 C# 中做类似的事情,它似乎不受支持:

//Doesn't compile
ISet<string> set = new HashSet<>();
//Works OK
ISet<string> set2 = new HashSet<string>();

I get two compiler messages in C# from the above:我从上面的 C# 中得到两条编译器消息:

CS7003 Unexpected use of an unbound generic name CS7003 意外使用未绑定的通用名称

and

CS0266 Cannot implicitly convert type 'System.Collections.Generic.HashSet' to 'System.Collections.Generic.ISet'. CS0266 无法将类型“System.Collections.Generic.HashSet”隐式转换为“System.Collections.Generic.ISet”。 An explicit conversion exists (are you missing a cast?)存在显式转换(您是否缺少演员表?)

Is this feature genuinely not supported or am I just missing out on some C# syntax?是真的不支持此功能还是我只是错过了一些 C# 语法?

Thanks to a comment by canton7 we have an answer.感谢canton7的评论,我们有了答案。 The feature is not supported (yet) by C# but there are discussions around this in the pipelines. C#(尚)不支持该功能,但在管道中对此进行了讨论。 There are two possible directions this may be taken:这可能有两个可能的方向:

The Diamond Operator <>钻石操作员 <>

There is the proposal to use the diamond operator, exactly as stated in the question:有使用钻石运算符的建议,正如问题中所述:

https://github.com/dotnet/csharplang/issues/2935 https://github.com/dotnet/cshaplang/issues/2935

This would allow one to use Java-like syntax as above, where the reference type is an interface and the object type is some implementation of that interface.这将允许使用类似 Java 的语法,其中引用类型是一个接口,而 object 类型是该接口的一些实现。

The new Keyword新关键字

There is the proposal to use the new keyword:有使用new关键字的建议:

https://github.com/dotnet/csharplang/issues/100 https://github.com/dotnet/cshaplang/issues/100

This proposal, for example, would allow the following:例如,该提案将允许以下内容:

Point p = new Point(1, 2);

To be replaced with simply:简单地替换为:

Point p = new (1, 2);

Indeed, for generic types, both the type, and any generics that come with it, would be inferred.事实上,对于泛型类型,类型和任何 generics 都会被推断出来。 So we could do, for example:所以我们可以这样做,例如:

Hashset<string> set = new();

This initially seems more powerful than the diamond operator proposal.这最初似乎比钻石运营商提案更强大。 However, returning to the original example from the question, this feature, if developed, would not work for the case where the reference type is an interface.但是,从问题返回到原始示例,如果开发此功能,将不适用于引用类型是接口的情况。 In that case, it would be unclear which object to create from new .在这种情况下,不清楚要从new创建哪个 object 。 For example if we did:例如,如果我们这样做:

ISet<string> set = new ();

Then how would the compiler know which implementation of the ISet interface to use to instantiate an object of that type?那么编译器如何知道ISet接口的哪个实现用于实例化该类型的 object 呢? So even if this feature were implemented, it wouldn't solve the use case where the reference type is an interface.因此,即使实现了此功能,也无法解决引用类型为接口的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM