how to upcast object array to another type of object array in C#?

前端 未结 3 961
耶瑟儿~
耶瑟儿~ 2021-01-22 12:29

I want to upcast object array to different array of different object type like below

object[] objects; // assuming that it is non-empty

CLassA[] newObjects = obj

3条回答
  •  悲&欢浪女
    2021-01-22 12:57

    As this post suggests, you may be able to do the following trick (untested):

    newObjects = (ClassA[])(object)objects;
    

    Note that in C# 4.0 you won't need to cast, you will be able to directly assign newObjects = objects.

提交回复
热议问题