gpt4 book ai didi

c# - 使用 Linq 的隐式转换

转载 作者:行者123 更新时间:2023-11-30 21:53:16 24 4
gpt4 key购买 nike

假设我有一个整数列表 ( var identifiers = Enumerable.Empty<int>() )。

通过这个列表,我可以将单个项目转换为另一种类型:

var castedIdentifiersLong = identifiers.Cast<long>();
var castedIdentifiersString = identifiers.Cast<string>();

我们可以使用Select<TSource, TTarget>()使用隐式转换:

var mappedIdentifiersLong = identifiers.Select<int, long>(x => x);
var mappedIdentifiersString = identifiers.Select<int, string>(x => x);

显然最后一条语句失败了,因为int不能隐式转换为 string .这是故意的。

有没有一种方法可以定义一个扩展方法(比如 CastImplicitly<T>),我只能在其中定义两个泛型类型之一,它会从可枚举的源中找出第一个类型?

var unwanted = identifiers.ImplicitCast<string>();
var wanted = identifiers.ImplicitCast<long>();

在这种情况下 unwanted甚至不应该编译,因为 int不能隐式转换为 string .但另一方面,wanted应该编译,因为它可以隐式转换为 long .

最佳答案

Is there a way that I can define an extension method (say CastImplicitly<T>), where I can only define one of the two generic types and it would figure out the first type from the source enumerable?

如果输入是通用的,则不是。编译器不能部分推断泛型参数——您要么必须指定所有泛型参数,要么不指定任何泛型参数,让编译器推断。

即使您可以,编译器仍然不允许泛型类型之间的隐式转换。

如果您希望编译器在编译时识别无效的转换,您可以进行显式转换:

var mappedIdentifiersString = identifiers.Select(x => (string)x);  // fails at compile time if x is an int.

它可能无法在编译时捕获所有 可能的无效转换(例如,在编译时始终允许向/从 object 进行转换),但它确实会因您的特定 int 而失败。至 string场景。

关于c# - 使用 Linq 的隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com