动态数组c#...... [英] dynamic array c#......

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

问题描述

如何使用C#创建动态数组?

解决方案

如果动态在这种情况下意味着匿名数据,您可以这样做:

  var  items =  new  [] 
{
new {user = some user },
new {user = 其他用户}
};



如果您只想将变量称为动态变量,则可以执行以下操作:

 动态 items =  new  ExpandoObject(); 
项[ 0 ] = 部分用户;
项目[ 1 ] = 其他一些user;


C#没有动态数组类型或任何等效的VB Redim语句。

但是,您可以使用List< T>作为动态数组,假设它支持索引访问:

 List< string> list =  new  List< string>(); 
list.Add( Hello);
list.Add( Goodbye);
Console.WriteLine(list [ 1 ]);


你可以这样做制作数组列表



在数组列表中添加元素:



int a = 5;

strings =Crunchmodo;

newlist.Add(a);

newlist.Add(s);

/ /这行我们添加了DateTime类的对象

newlist.Add(DateTime.Now);

现在我们添加了三个不同数据类型的元素和一个DateTime类对象。数组列表中的Add()函数将新项添加到数组列表。



获取数组列表的大小:



int size = newlist.Count;

通过使用属性Count of Array List对象,我们可以得到Array List的大小。因为我们添加了三个项目,因为数组列表的大小是3.大小将存储在一个名为size的变量中。



从数组列表中访问项目: br $>


主要任务是从阵列列表中访问这些项目。在访问我们必须将每个项目转换为正确的数据类型或对象的项目时,必须记住一件事。

因为我们先插入整数,所以我们可以从Array List访问它,如下所示:



// 0基于索引

int b =(int)newlist [0];

string t = newlist [1] as string;

DateTime dt =(DateTime)newlist [2];





清算阵列表:



我们可以按如下方式清除和删除列表的所有元素。



newlist.Clear();



来自

http://crunchmodo.com/c-arraylist/ [ ^ ]

How to make a dynamic array with C#?

解决方案

If the "dynamic" in this case means anonymous data you can do as:

var items = new[]
{
    new { user = "some user" }, 
    new { user = "other user" }
};


If you just want call the variable as dynamic, you can do this:

dynamic items = new ExpandoObject();
items[0] = "Some user";
items[1] = "Some other user";


C# does not have a dynamic array type, or any equivalent of the VB Redim statement.
However, you can use a List<T> as a dynamic array, given that it supports indexed access:

List<string> list = new List<string>();
list.Add("Hello");
list.Add("Goodbye");
Console.WriteLine(list[1]);


You can do this making array list

Adding Elements in the Array List:

int a=5;
strings="Crunchmodo";
newlist.Add(a);
newlist.Add(s);
//this line we have added object of DateTime class
newlist.Add(DateTime.Now);
Now we have added three elements of different data type and one object of DateTime class. Add() function from the Array List adds new item to the Array List.

Getting Size of the Array List:

int size=newlist.Count;
By using the property Count of Array List object we can get the size of Array List. As we have added three items because size of the Array List is 3. The size will be stored in a variable named size.

Accessing Items from the Array List:

The main task is to access the items back from Array List. One thing must be kept in mind while accessing the item that we have to cast each item into its proper data type or object.
As we have inserted integer first so we can access it from Array List as follows:

//0 based indexing
int b = (int)newlist[0];
string t=newlist[1] as string;
DateTime dt=(DateTime)newlist[2];


Clearing Array List:

We can clear and delete the all elements of list as follows.

newlist.Clear();

its from
http://crunchmodo.com/c-arraylist/[^]


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

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