C # Language Collection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.