Switch usage
int x = Int. Parse (Console.ReadLine ());
Switch (x) {
Case 1:
Console.WriteLine ("This is 1");
Break
Case 2:
Console.WriteLine ("This is 2");
Break
Case 3:
Console.WriteLine ("This is 3");
Break
}
Bubble sort
Int[] A = new int[] {1, 5, 6, 3, 4};
for (int i = 0; i < a.length;i++) {
for (int t = i+1; t < a.length;t++) {focus
if (A[i]>a[t]) {
int s = a[i];
A[i] = a[t];
A[t] = s;
}
}
}
for (int i = 0; i< a.length;i++)
{
Console.WriteLine (A[i]);
}
General Collection
ArrayList p = new ArrayList ();
P.add ("123");
P.add (456);
P.add (TRUE);
P.add ("word");
for (int i = 0; i < P.count; i++)
{
Console.WriteLine (P[i]. ToString () + "" + p[i]. GetType (). ToString ());
}
foreach (var x in P)
{
Console.WriteLine (x + "space" + X.gettype ());
}
X.tostring () + "" + X.gettype (). ToString ()
Generic collection
list<string> str_list = new list<string> ();
Str_list. ADD ("a");
Str_list. ADD ("B");
Str_list. ADD ("C");
Str_list. ADD ("D");
foreach (string x in Str_list) {
Console.WriteLine (x);
}
Hash table
Hashtable ht = new Hashtable ();
Ht. ADD ("1", "a");
Ht. ADD ("2", "B");
Ht. ADD ("3", false);
Ht. ADD ("x", 3.14);
Console.WriteLine (Ht[2]);
foreach (var x in HT) {
Console.WriteLine (x);
}
Word Typical
Dictionary<string, int> m = new dictionary<string, int> ();
M.add ("A", 3);
M.add ("B", 4);
M.add ("C", 5);
M.add ("D", 6);
foreach (var x in m.values)
{
Console.WriteLine (x);
}
M.values Print out "3456" M.keys print out "ABCD"
Queue
Queue que = new queue ();
Que. Enqueue ("Zhang San");
Que. Enqueue ("John Doe");
Que. Enqueue ("Harry");
Que. Enqueue ("Zhao Liu");
Console.WriteLine ("The current length is" +que.) Count);
Console.WriteLine (Que. Dequeue ());
Console.WriteLine ("Now the length is" + Que. Count);
Output Zhang San, now the length is 3
Stack
Stack st = new stack ();
St. Push ("a");
St. Push ("B");
St. Push ("C");
St. Push ("D");
Console.WriteLine (St. Count);
Console.WriteLine (St. Pop ());
Console.WriteLine (St. Count);
Output d, length is 3
C # Language Collection