关于一些概念的理解


来自csdn的HashTable的简单应用


using System;
using System.Collections;
public class SamplesHashtable  {

   
public static void Main()  {

      
// Creates and initializes a new Hashtable.
      Hashtable myHT = new Hashtable();
      myHT.Add(
"First""Hello");
      myHT.Add(
"Second""World");
      myHT.Add(
"Third""!");

      
// Displays the properties and values of the Hashtable.
      Console.WriteLine( "myHT" );
      Console.WriteLine( 
"  Count:    {0}", myHT.Count );
      Console.WriteLine( 
"  Keys and Values:" );
      PrintKeysAndValues( myHT );
   }


   
public static void PrintKeysAndValues( Hashtable myList )  {
      IDictionaryEnumerator myEnumerator 
= myList.GetEnumerator();
      Console.WriteLine( 
"\t-KEY-\t-VALUE-" );
      
while ( myEnumerator.MoveNext() )
         Console.WriteLine(
"\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value);
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

myHT
  Count:    3
  Keys and Values:
    -KEY-    -VALUE-
    Third:    !
    Second:    World
    First:    Hello
*/


以后根据应用慢慢完善
csdn上面关于   Dictionary <>的一些基本应用
using System;
using System.Collections.Generic;

public class Example
{
    
public static void Main()
    {
        
// Create a new dictionary of strings, with string keys.
        
//
        Dictionary<stringstring> openWith = 
            
new Dictionary<stringstring>();

        
// Add some elements to the dictionary. There are no 
        
// duplicate keys, but some of the values are duplicates.
        openWith.Add("txt""notepad.exe");
        openWith.Add(
"bmp""paint.exe");
        openWith.Add(
"dib""paint.exe");
        openWith.Add(
"rtf""wordpad.exe");

        
// The Add method throws an exception if the new key is 
        
// already in the dictionary.
        try
        {
            openWith.Add(
"txt""winword.exe");
        }
        
catch (ArgumentException)
        {
            Console.WriteLine(
"An element with Key = \"txt\" already exists.");
        }

        
// The Item property is another name for the indexer, so you 
        
// can omit its name when accessing elements. 
        Console.WriteLine("For key = \"rtf\", value = {0}."
            openWith[
"rtf"]);

        
// The indexer can be used to change the value associated
        
// with a key.
        openWith["rtf"= "winword.exe";
        Console.WriteLine(
"For key = \"rtf\", value = {0}."
            openWith[
"rtf"]);

        
// If a key does not exist, setting the indexer for that key
        
// adds a new key/value pair.
        openWith["doc"= "winword.exe";

        
// The indexer throws an exception if the requested key is
        
// not in the dictionary.
        try
        {
            Console.WriteLine(
"For key = \"tif\", value = {0}."
                openWith[
"tif"]);
        }
        
catch (KeyNotFoundException)
        {
            Console.WriteLine(
"Key = \"tif\" is not found.");
        }

        
// When a program often has to try keys that turn out not to
        
// be in the dictionary, TryGetValue can be a more efficient 
        
// way to retrieve values.
        string value = "";
        
if (openWith.TryGetValue("tif"out value))
        {
            Console.WriteLine(
"For key = \"tif\", value = {0}.", value);
        }
        
else
        {
            Console.WriteLine(
"Key = \"tif\" is not found.");
        }

        
// ContainsKey can be used to test keys before inserting 
        
// them.
        if (!openWith.ContainsKey("ht"))
        {
            openWith.Add(
"ht""hypertrm.exe");
            Console.WriteLine(
"Value added for key = \"ht\": {0}"
                openWith[
"ht"]);
        }

        
// When you use foreach to enumerate dictionary elements,
        
// the elements are retrieved as KeyValuePair objects.
        Console.WriteLine();
        
foreach( KeyValuePair<stringstring> kvp in openWith )
        {
            Console.WriteLine(
"Key = {0}, Value = {1}"
                kvp.Key, kvp.Value);
        }

        
// To get the values alone, use the Values property.
        Dictionary<stringstring>.ValueCollection valueColl =
            openWith.Values;

        
// The elements of the ValueCollection are strongly typed
        
// with the type that was specified for dictionary values.
        Console.WriteLine();
        
foreachstring s in valueColl )
        {
            Console.WriteLine(
"Value = {0}", s);
        }

        
// To get the keys alone, use the Keys property.
        Dictionary<stringstring>.KeyCollection keyColl =
            openWith.Keys;

        
// The elements of the KeyCollection are strongly typed
        
// with the type that was specified for dictionary keys.
        Console.WriteLine();
        
foreachstring s in keyColl )
        {
            Console.WriteLine(
"Key = {0}", s);
        }

        
// Use the Remove method to remove a key/value pair.
        Console.WriteLine("\nRemove(\"doc\")");
        openWith.Remove(
"doc");

        
if (!openWith.ContainsKey("doc"))
        {
            Console.WriteLine(
"Key \"doc\" is not found.");
        }
    }
}

/* This code example produces the following output:

An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Key = "tif" is not found.
Key = "tif" is not found.
Value added for key = "ht": hypertrm.exe

Key = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = winword.exe
Key = doc, Value = winword.exe
Key = ht, Value = hypertrm.exe

Value = notepad.exe
Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe
Value = hypertrm.exe

Key = txt
Key = bmp
Key = dib
Key = rtf
Key = doc
Key = ht

Remove("doc")
Key "doc" is not found.
 
*/

产生0.9到1.1 的随机数
 Random ro = new Random();
            
decimal aaa = Convert.ToDecimal (ro.Next(Convert.ToInt32(0.9 * 100), Convert.ToInt32(1.1 * 100)));
            
decimal iresult = aaa / 100//取小数点后两位

posted @ 2007-08-30 10:38  jhtchina  阅读(339)  评论(0)    收藏  举报