首页 > 学院 > 开发设计 > 正文

c# arraylist functions

2019-11-18 17:17:51
字体:
来源:转载
供稿:网友

When you put then in the array list you could check to see if the item already exists.  This code snippet will check to see if the string is already in the array and will only add it when the item doesn't already exist in the list.

static void Main( string[] args )
{
 ArrayList list = new ArrayList();

 AddToList( list, "Table1" );
 AddToList( list, "Table4" );
 AddToList( list, "Table1" );
 AddToList( list, "Table3" );
 AddToList( list, "Table2" );
 AddToList( list, "Table2" );

foreach ( string s in list ) //this will loop the collection to show there are no duplicates
 {
  Console.WriteLine( s );
 }
}

PRivate static void AddToList( ArrayList list, string p )
{
 if ( list.Contains( p ) == false )
  list.Add( p );
}


 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表