具有强类型的动态数组? [英] Dynamic array with strong types?

查看:90
本文介绍了具有强类型的动态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有没有办法在C#中使用强类型动态数组?


ArrayList要求你每次都要施放你想要访问一个

元素

对象[]不是动态的


我已经使用了写hacky Add( )对象的函数[](下面)


另一种选择是派生一个ArrayListBool等..来自

ArrayList其中索引器执行了强制转换并返回了一个强大的类型


但是我会有比这些更好的方法吗?


我想要一个等同于C ++ std ::向量< type>




框架中删除它似乎是一个令人惊讶的疏忽


Am我在这里遗漏了一些明显的东西?


Vin

解决方案

>我已经选择了写一个hacky Add( )对象的功能[](下)


忘记代码


private o bject [] AddToArray(object [] aSrc,object aNew)

{

if(aSrc.GetType()== typeof(int []))

{

int [] aArray = new int [aSrc.GetLength(0)+ 1];

aSrc.CopyTo(aArray,0);

aSrc = aArray;

aSrc [aSrc.GetLength(0) - 1] =(int)aNew;

}

//等等其他类型

返回aSrc;

}


Hello Vincent,


您想要的是通用类型,但它们只能在下一版本的框架中使用。

在.net 2.0中,您可以创建一个ArrayList< bool> ;,在1.1中你必须像你一样扩展提供的ArrayList或自己编写。

我建议你下载Codesmith和一些集合?的模板,它们将帮助你自动创建强类型集合。


Codesmith: http:// www。 ericjsmith.net/codesmith/

Codesmith的集合模板: http://www.kynosarges.de /Templates.html

问候,

-----

Ariel Popovsky

< blockquote class =post_quotes>

有没有办法在C#中使用强类型动态数组?

每次要访问时,ArrayList都要求你进行转换
元素
对象[]不动态
我已经为对象[](下面)编写了一个hacky Add()函数

另一个替代方案从ArrayList中得到一个ArrayListBool等..其中索引器做了强制转换并返回了强类型

但是我会有比这些更好的方法吗?

我想要一个等同于C ++ std :: vector< type>

将这个从
框架中删除似乎是一个令人惊讶的疏忽

>我错过了一些明显的东西吗?

Vin




周五,2004年11月5日04:42:20 -0800,Ariel Popovsky

< AP ******* @ hotspamNOmail.com>写道:

Hello Vincent,

你想要的是通用类型,但它们只能在下一版本的框架中使用。
在.net 2.0中你可以创建一个ArrayList< bool>,在1.1中你必须像你一样扩展提供的ArrayList或自己编写。
我建议你下载Codesmith和一些集合的模板来帮助你自动化强类型集合的创建。

Codesmith: http:// www.ericjsmith.net/codesmith/
Codesmith的集合模板: http://www.kynosarges.de/Templates.html

问候,
-----
Ariel Popovsky



看起来仍然是一个非常糟糕的疏忽\决定


哦,至少我知道我不是只是错过了明显的


谢谢,Vin


Hi,

Is there any way of having a strongly typed dynamic array in C#?

ArrayList requires you to cast every time you want to access an
element
object[] isn''t dynamic

I have resorted writing a hacky Add() function for object[] (below)

the other alternative was deriving a ArrayListBool etc.. from
ArrayList where the indexer did the cast and returned a strong type

but I would have though there must be a better way than these?

I want an equivalent to the C++ std::vector<type>

It seems like an astonishing oversight to leave that out of the
framework

Am I missing something obvious here?

Vin

解决方案

>I have resorted writing a hacky Add() function for object[] (below)

Forgot the code

private object[] AddToArray(object[] aSrc, object aNew)
{
if (aSrc.GetType() == typeof(int[]))
{
int[] aArray = new int[aSrc.GetLength(0) + 1];
aSrc.CopyTo(aArray, 0);
aSrc = aArray;
aSrc[aSrc.GetLength(0) - 1] = (int)aNew;
}
// and so on for other types
return aSrc;
}


Hello Vincent,

What you want are generic types, but they will be available only in the next version of the framework.
In .net 2.0 you can create an ArrayList<bool>, in 1.1 you have to extend the provided ArrayList as you did or write your own.
I recommend you download Codesmith and some collection?′s templates that will help you automate the creation of strongly typed collections.

Codesmith: http://www.ericjsmith.net/codesmith/
Collection templates for Codesmith: http://www.kynosarges.de/Templates.html
Greetings,
-----
Ariel Popovsky

Hi,

Is there any way of having a strongly typed dynamic array in C#?

ArrayList requires you to cast every time you want to access an
element
object[] isn''t dynamic
I have resorted writing a hacky Add() function for object[] (below)

the other alternative was deriving a ArrayListBool etc.. from
ArrayList where the indexer did the cast and returned a strong type

but I would have though there must be a better way than these?

I want an equivalent to the C++ std::vector<type>

It seems like an astonishing oversight to leave that out of the
framework

Am I missing something obvious here?

Vin




On Fri, 05 Nov 2004 04:42:20 -0800, Ariel Popovsky
<ap*******@hotspamNOmail.com> wrote:

Hello Vincent,

What you want are generic types, but they will be available only in the next version of the framework.
In .net 2.0 you can create an ArrayList<bool>, in 1.1 you have to extend the provided ArrayList as you did or write your own.
I recommend you download Codesmith and some collection′s templates that will help you automate the creation of strongly typed collections.

Codesmith: http://www.ericjsmith.net/codesmith/
Collection templates for Codesmith: http://www.kynosarges.de/Templates.html
Greetings,
-----
Ariel Popovsky



Still seems like an very bad oversight\decision

Oh well at least I know I amn''t just missing the obvious

Thanks, Vin


这篇关于具有强类型的动态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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