如何在C#中的单个数组中添加不同类型的对象? [英] How to add different types of objects in a single array in C#?

查看:214
本文介绍了如何在C#中的单个数组中添加不同类型的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算用C#重写Python Tile Engine。它使用所有游戏对象的列表,并将它们呈现在屏幕上。我的问题是,与在Python中可以向数组添加几乎所有内容(例如 x = [ jj,1,2.3,'G',foo] )不同,您可以只能在C#数组中添加一种类型的对象
int [] x = {1,2,3}; )。是否有任何动态数组(类似于 ArrayList()类)或某些允许将不同类型打包到单个数组中的东西?因为所有游戏对象都是单独的类。

I am planning to rewrite my Python Tile Engine in C#. It uses a list of all the game objects and renders them on the screen. My problem is that unlike in Python where you can add almost anything to an array (e.g x = ["jj" , 1, 2.3, 'G', foo]) you can add only one type of objects in a C# array (int[] x = {1,2,3};) . Are there any dynamic arrays (similar to the ArrayList() class) or something which allows you to pack different types into a single array? because all the game objects are individual classes.

推荐答案

非常简单-创建Object类的数组并将任何东西分配给该数组。

Very simple—create an array of Object class and assign anything to the array.

Object[] ArrayOfObjects = new Object[] {1,"3"}

这篇关于如何在C#中的单个数组中添加不同类型的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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