集合与泛型之间的基本区别是什么 [英] What is the basic difference between Collection and Generics

查看:86
本文介绍了集合与泛型之间的基本区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

集合和泛型之间的基本区别是什么?

What is the basic difference between Collection and Generics?

推荐答案

简而言之,泛型和非泛型集合之间的基本区别:

-非通用集合-这些集合可以容纳不同数据类型的元素.它包含所有元素作为对象类型.
因此,它包括类型转换的开销.

-通用集合-这些集合可以保存相同类型的数据,我们可以决定集合可以保存的数据类型.

泛型集合的一些优点-类型安全,安全,可减少类型转换的开销.

您可以在此处阅读更多有关泛型的信息. [
Briefly, the basic difference between generic and non-generic collections:

- Non-Generic collections - These are the collections that can hold elements of different data types. It holds all elements as object type.
So it includes overhead of type conversions.

- Generic collections - These are the collections that can hold data of same type and we can decide what type of data that collections can hold.

Some advantages of generic collections - Type Safe, Secure, reduced overhead of type conversions.

You can read more about generics here[^]. :)

Regards


也许与使用Google [ ^ ]或Bing [
Maybe it is similar to the difference between, say, searching the info with Google [^] or Bing [^]?
:rolleyes:


< b>< u>这里是通用集合的典范
</u></b>


公共类MyCustomList< MYTYPE>
{
私有ArrayList m_list = new ArrayList();
public int Add(myType value)
{
返回m_list.Add(value);
}

公共无效Remove(myType值)
{
m_list.Remove(value);
}

public myType this [int index]
{
获取
{
返回(myType)m_list [index];
}
设置
{
m_list [index] =值;
}
}
}
<b><u>Here is the exmaple of the Generic collections
</u></b>


public class MyCustomList<MYTYPE>
{
private ArrayList m_list = new ArrayList();
public int Add(myType value)
{
return m_list.Add(value);
}

public void Remove(myType value)
{
m_list.Remove(value);
}

public myType this[int index]
{
get
{
return (myType)m_list[index];
}
set
{
m_list[index] = value;
}
}
}


这篇关于集合与泛型之间的基本区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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