在 C# 的语法,可以提供自动将某个类的数组自动转这个类的基类数组的方法,但是这样的转换在 Resharper 会提示 Co-variant array conversion 这是什么问题?

在 C# 使用强类型,也就是默认在某个类型的数组里面,不能存放不继承当前数组类型的类。在自动转换基类的数组的时候,实际的对象还是原来的类。

如我可以使用下面的代码将 string 数组转换为 object 数组

            string[] foo = new[]
            {
                "lindexi",
                "欢迎访问我博客 https://blog.lindexi.com/ 里面有大量 UWP WPF 博客"
            };

            object[] f1 = foo;

但是这不代表 f1 也是 object 数组,只是用起来可以作为 object 数组用,如果我存放一个不是继承字符串的类,那么将会提示 System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array. 因为这个数组是强数组,不能加入和数组定义不相同的

            string[] foo = new[]
            {
                "lindexi",
                "欢迎访问我博客 https://blog.lindexi.com/ 里面有大量 UWP WPF 博客"
            };

            object[] f1 = foo;

            f1[1] = 10;// System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.

object[] f1 = foo 有 Resharper 提示 Co-variant array conversion can cause run-time exception 告诉你不建议这样写

但是如果我定义的时候,将 foo 修改为 object 数组就没有这个问题

            object[] foo = new object[]
            {
                "lindexi",
                "欢迎访问我博客 https://blog.lindexi.com/ 里面有大量 UWP WPF 博客"
            };

            object[] f1 = foo;

            f1[1] = 10;

这个方法就是将这个数组定义的类尽可能底层这样就可以让数组加入继承定义的数组的类的

但是更多的是在 Linq 的时候使用,如我从一个 Foo 方法里面拿到了字符串数组,此时我需要将这个数组转换为 object 数组,那么也会有相同提示

            object[] foo = new List<string>
            {
                "lindexi",
                "欢迎访问我博客 https://blog.lindexi.com/ 里面有大量 UWP WPF 博客"
            }.ToArray();
            // Resharper 提示 Co-variant array conversion can cause run-time exception 因为 ToArray 返回的是 string[] 也就是通过 foo 拿到的是强数组

需要解决这个问题可以使用 ToArray 的方法,让返回的是 object 数组

            object[] foo = new List<string>
            {
                "lindexi",
                "欢迎访问我博客 https://blog.lindexi.com/ 里面有大量 UWP WPF 博客"
            }.ToArray<object>();

Co-variant array conversion

Eric Lippert post


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/dotnet-%E6%95%B0%E7%BB%84%E8%87%AA%E5%8A%A8%E8%BD%AC%E5%9F%BA%E7%B1%BB%E6%95%B0%E7%BB%84%E6%8F%90%E7%A4%BA-Co-variant-array-conversion-%E6%98%AF%E4%BB%80%E4%B9%88%E9%97%AE%E9%A2%98.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号

欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利