这个结构如何定义?

dongjtlhd 2008-08-01 10:41:45
如何在c#中定义一个结构,该结构中有一个成员是数组指针,之所以用指针是因为接收的数组长度不固定,刚学c#不知道这个如何实现。
c的定义
typedef struct {
int x;
int y;
int *arrP;
};
arrP接收一个数组的元素,所以不能定义成固定长度的数组。不知道c#里面是如何实现的。
...全文
80 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ClementDik 2008-08-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 willqiangliu 的回复:]
List跟ArrayList有什么区别吗?
[/Quote]

List<int> 的各个元素都是int类型的。使用时不需要转换。
而ArrayList的各个元素都是object类型的。使用时需要判断类型并进行类型转换。

如:

List<int> list = new List<int>();
ArrayList arr = new ArrayList();

for (int i = 0; i < 10; i++)
{
list.Add(i);
ArrayList.Add(i);
}

int y = list[3]; // 这里不需要类型转换
int x = (int)arr[3]; // 这里必须要进行强制转换,否则编译会出错。
昵称好难找哈 2008-08-01
  • 打赏
  • 举报
回复
List跟ArrayList有什么区别吗?
zhengyh5339 2008-08-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jacklau88 的回复:]
struct NewType
{
int x;
int y;
List <int> arrP;
}
[/Quote]
baihe_591 2008-08-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 gomoku 的回复:]
struct
{
public int x;
public int y;
public IntPtr arrP;
}
[/Quote]

指针可用intptr代替,到时候读取数据时:
Marshal.PtrToStructure(intptr,type);
jacklau88 2008-08-01
  • 打赏
  • 举报
回复
struct NewType
{
int x;
int y;
List<int> arrP;
}

dongjtlhd 2008-08-01
  • 打赏
  • 举报
回复
谢谢大家,先去试试!
Konig2007 2008-08-01
  • 打赏
  • 举报
回复
用ArrayList首先要添加命名空间System.Collections
声明: ArrayList newList=new ArrayList();
添加一个元素: ArrayList.Add(item);
这个元素item可以是各种类型的对象。
yyq136 2008-08-01
  • 打赏
  • 举报
回复
struct
{
public int x;
public int y;
public ArrayList arrP;
}
dongjtlhd 2008-08-01
  • 打赏
  • 举报
回复
用arraylist如何定义?
ClementDik 2008-08-01
  • 打赏
  • 举报
回复


struct NewType
{
int x;
int y;
List<int> arrP;
}

fuda_1985 2008-08-01
  • 打赏
  • 举报
回复
可以用动态数组arraylist代替~
gomoku 2008-08-01
  • 打赏
  • 举报
回复
struct
{
public int x;
public int y;
public IntPtr arrP;
}

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧