Ten special sets and functions

Source: Internet
Author: User

First, Stack collection

Stack: Stack, FIFO, an assignment, one value, in order .

Properties and methods:

. Count takes the number of elements inside the collection

. Push pushes elements one by one into the collection

Pop sets the element one pop-up collection

. Clear to empty the collection

Example 1

Special collection. Stack. Hay heap, advance to the last only to come out

Stack Hay Heap
Advanced first out push out pop
Initialization
Stack ss = new stack ();
Ss. Push (1);
Ss. Push (2);
Ss. Push (3);
Ss. Push (4);
Ss. Push (5);
Console.WriteLine (ss. Pop ());//eject the last element into the collection
See only the last value that goes in
Console.WriteLine (ss. Peek ());

Error no index number
Console.WriteLine (Ss[0]);
foreach (Object AA in SS)
{
Console.Write (AA);
}
Emptying the collection
Ss. Clear ();
Console.ReadLine ();

Queue Queues Collection

FIFO, an assignment of one to one value, in order.

Properties and Methods

. Count takes the number of elements inside the collection

. Enqueue into the queue collection

. Dequeue Out Queue Collection

. Clear (method) empties the collection

Queue Queues Collection FIFO first
No index
Initialization
Queue que = new queue ();
Que. Enqueue (1);
Que. Enqueue (2);
Que. Enqueue (3);
Que. Enqueue (4);
Que. Enqueue (5);

Que. Dequeue ();
Console.WriteLine (Que. Peek ());//View a separate
foreach (Object aa in Que)
{
Console.WriteLine (AA);
}
Console.ReadLine ();

Hashtable Hash Table
Advanced after out, one assignment, but only together to value
Properties and Methods

. Add (,) adds key and element
. Remove () Removes the element inside the parentheses
. Contains () Determine whether there are bracketed elements in the collection
. Count counts the number of elements in the collection

Hashtable ht = new Hashtable ();
Ht. ADD (0, "Coca-Cola");
Ht. ADD (1, "Sprite");
Ht. ADD (2, "Pepsi");
Ht. ADD (3, "Fanta");
Ht. ADD (4, "Wang Lao Ji");


IDictionaryEnumerator ID = ht. GetEnumerator ();//reading of enumeration type types read into table style, horizontal arrangement
while (ID. MoveNext ())
{
Console.WriteLine (ID. key+ "\ t" +id. Value);
}

Ht. Remove (3);
Console.WriteLine (HT. Contains (2));

foreach (int i in HT. Keys)
{
Console.WriteLine (i);
}
foreach (object B in Ht. Values)
{
Console.WriteLine (b);
}
Console.ReadLine ();

Function

A larger program should generally be divided into a number of blocks, each of which is used to implement a specific function. All high-level languages have the concept of subroutines, which implements the function of modules with subroutines. In the C # language, the role of a subroutine is composed of a main function and a number of functions. The main function is used to hang other functions, and other functions can be called from each other. The same function can be called any number of times by one or more functions.

In the program design, some common function modules are often written into functions, which are put in the library for public use. Be good at using functions to reduce the amount of repetitive programming segments.

Function four elements: input (there are no parameters inside the parentheses) output function body {} Curly braces include the statement function name (from the name)

Example:

Class Program
{
public void Dayin ()
{
Console.WriteLine ("Hello!");
}
static void Main (string[] args)
{
Call function Initialization
Program Hanshu = new program ();
Hanshu.dayin ();
Console.ReadLine ();

Random ran=new random ();
Ran. Next ();

Class Program
{
public void Leijia ()
{
First: No parameters, no return value
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
Console.WriteLine (sum);
Console.ReadLine ();

}
Second, there is no return value, but there are parameters
public void Leijia (int a)
{
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
Console.WriteLine (sum);
Console.ReadLine ();
}
The third type, with a return value, has parameters
public int Leijia1 (int b)
{
int sum = 0;
for (int i = 1; I <=b; i++)
{
sum + = i;
}

return sum;
}
The fourth type has a return value, no parameters
public int Leijia2 ()
{
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; I <=b; i++)
{
sum + = i;
}

return sum;

static void Main (string[] args)
{
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());

Program Hanshu = new program ();
Hanshu.leijia ();

Program Hanshu = new program ();
Hanshu.leijia (a);

Program Hanshu = new program ();
int sum= Hanshu. Leijia1 (a);
Console.WriteLine (sum);
Console.ReadLine ();


Program Hanshu = new program ();
int sum= Hanshu. LEIJIA2 ();
Console.WriteLine (sum);
Console.ReadLine ();

Random ran = new random ();
int aa = ran. Next ();


Write a function to compare size
Three number comparison size
Double a=3;
Double b=5,c=2;
Program Hanshu=new program ();
Double Max=hanshu. Max (Hanshu. Max (A, B), c);
Console.WriteLine (max);
Console.ReadLine ();

Ten special sets and functions

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.