C#将HashTable中键列表或值列表复制到一维数组的方法

 更新时间:2015年04月28日 11:56:04   作者:heishui  
这篇文章主要介绍了C#将HashTable中键列表或值列表复制到一维数组中方法,涉及C#操作HashTable的相关技巧,需要的朋友可以参考下

本文实例讲述了C#将HashTable中键列表或值列表复制到一维数组的方法。分享给大家供大家参考。具体如下:

下面的示例说明如何将 Hashtable 中键的列表或值的列表复制到一维 Array 中。

using System;
using System.Collections;
public class SamplesHashtable {
 public static void Main() {
  // Creates and initializes the source Hashtable.
  Hashtable mySourceHT = new Hashtable();
  mySourceHT.Add( "A", "valueA" );
  mySourceHT.Add( "B", "valueB" );
  // Creates and initializes the one-dimensional target Array.
  String[] myTargetArray = new String[15];
  myTargetArray[0] = "The";
  myTargetArray[1] = "quick";
  myTargetArray[2] = "brown";
  myTargetArray[3] = "fox";
  myTargetArray[4] = "jumped";
  myTargetArray[5] = "over";
  myTargetArray[6] = "the";
  myTargetArray[7] = "lazy";
  myTargetArray[8] = "dog";
  // Displays the values of the target Array.
  Console.WriteLine( "The target Array contains the following before:" );
  PrintValues( myTargetArray, ' ' );
  // Copies the keys in the source Hashtable to the target Hashtable, starting at index 6.
  Console.WriteLine( "After copying the keys, starting at index 6:" );
  mySourceHT.Keys.CopyTo( myTargetArray, 6 );
  // Displays the values of the target Array.
  PrintValues( myTargetArray, ' ' );
  // Copies the values in the source Hashtable to the target Hashtable, starting at index 6.
  Console.WriteLine( "After copying the values, starting at index 6:" );
  mySourceHT.Values.CopyTo( myTargetArray, 6 );
  // Displays the values of the target Array.
  PrintValues( myTargetArray, ' ' );
 }
 public static void PrintValues( String[] myArr, char mySeparator ) {
  for ( int i = 0; i < myArr.Length; i++ )
   Console.Write( "{0}{1}", mySeparator, myArr[i] );
  Console.WriteLine();
 }
}
/* 
This code produces the following output.
The target Array contains the following before:
 The quick brown fox jumped over the lazy dog
After copying the keys, starting at index 6:
 The quick brown fox jumped over B A dog
After copying the values, starting at index 6:
 The quick brown fox jumped over valueB valueA dog
*/

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • C# Windows Forms中实现控件之间的连接线的方法详解

    C# Windows Forms中实现控件之间的连接线的方法详解

    这篇文章主要为大家详细介绍了如何在C# Windows Forms应用程序中实现绘图工具中多个控件之间的连接线功能,文中的示例代码讲解详细,需要的可以参考下
    2024-02-02
  • C#9.0主要特性的一些想法

    C#9.0主要特性的一些想法

    这篇文章主要给大家介绍了关于C#9.0主要特性的一些想法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • c# HttpClient设置超时的步骤

    c# HttpClient设置超时的步骤

    这篇文章主要介绍了c# HttpClient设置超时的步骤,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C# WinForm调用Shell_NotifyIcon的示例代码

    C# WinForm调用Shell_NotifyIcon的示例代码

    这篇文章主要介绍了C# WinForm调用Shell_NotifyIcon的示例代码,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-11-11
  • Unity实现粒子光效导出成png序列帧

    Unity实现粒子光效导出成png序列帧

    这篇文章主要为大家详细介绍了Unity实现粒子光效导出成png序列帧,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C#编程中枚举类型的使用教程

    C#编程中枚举类型的使用教程

    这篇文章主要介绍了C#编程中枚举类型的使用,是C#入门学习中的基础知识,需要的朋友可以参考下
    2016-01-01
  • C#连接mariadb(MYSQL分支)代码示例分享

    C#连接mariadb(MYSQL分支)代码示例分享

    这篇文章主要介绍了C#连接mariadb的方法,和MySQL连接方式差不多,大家参考使用吧
    2013-11-11
  • C#中的矩形数组(多维数组)和锯齿数组的实现

    C#中的矩形数组(多维数组)和锯齿数组的实现

    本文主要介绍了C#中的矩形数组(多维数组)和锯齿数组的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • Unity使用EzySlice实现模型多边形顺序切割

    Unity使用EzySlice实现模型多边形顺序切割

    这篇文章主要为大家详细介绍了Unity使用EzySlice实现模型多边形顺序切割,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • C#中datatable去重的方法

    C#中datatable去重的方法

    这篇文章主要介绍了C#中datatable去重的方法,通过两种不同的方法对比分析了datatable去重的技巧,非常具有实用价值,需要的朋友可以参考下
    2014-10-10

最新评论