Collection class in C #

Source: Internet
Author: User

1. Define Collection class

The simplest way to define a Collection class in C # is to use the CollectionBase class in the System. Collection Library as the basic class.

Ii. Collection

The Collection class contains many methods and attributes. Here we will introduce the Add, Remove, Count, and Clear methods.

The Code is as follows:


Show sourceview sourceprint? 01 public class collection: CollectionBase

02 {

03 public void Add (object item)

04 {

05 InnerList. Add (item); // Add

06}

07 public void Remove (object item)

08 {

09 InnerList. Remove (item); // Delete

10}

11 public int Count ()

12 {

13 return InnerList. Count; // number of returned Elements

14}

15 public void Clear ()

16 {

17 InnerList. Clear (); // Clear all

18}


Iii. instantiate a class

In this example, two classes are instantiated: submittedTexts (the class for storing the exam) and outForChecking (the class for storing the exam after the exam is taken out)

The Code is as follows:


Show sourceview sourceprint? 1 collection submittedTexts = new collection ();

2 collection outForChecking = new collection ();


4. Example

This is a program used to submit a simulated exam. Function: (1) enter a name and exam number, and insert the exam to the submittedTexts collection;

(2) enter the name, delete the corresponding exam from submittedTexts, and insert the exam into the outForChecking set;

(3) enter the name, delete the exam from outForChecking, and put it back to submittedTexts;

(4) press the exit button to delete all the test papers from outForChecking and insert them all to submittedTexts.

The program interface is as follows:

 

1. Use the get method to obtain the value in textBox.

The Code is as follows:


Show sourceview sourceprint? 01 public string Nametext

02 {

03 get

04 {

05 return textBox1.Text;

06}

07}

08 public string Numtext

09 {

10 get

11 {

12 return textBox2.Text;

13}


2. Functions of buttons

The Code is as follows:


Show sourceview sourceprint? 01 submit exam

02 int Numtext2 = int. Parse (Numtext );

03 submittedTexts. Add (Nametext );

04 MessageBox. Show ("Exam submitted ");

05

06 obtain the exam (use collection INS in the collection class to determine whether the element is in the collection)

07 if (submittedTexts. Contains (Nametext ))

08 {

09 submittedTexts. Remove (Nametext );

10 outForChecking. Add (Nametext );

11 MessageBox. Show ("the exam has been taken out ");

12}

13 else

14 MessageBox. Show ("this exam is not available ");

15

16 total exam count

17 label4.Text = submittedTexts. Count (). ToString ();

18

19 put back the exam

20 if (outForChecking. Contains (Nametext ))

21 {

22 outForChecking. Remove (Nametext );

23 submittedTexts. Add (Nametext );

24 MessageBox. Show ("the exam has been put back ");

25}

26 else

27 MessageBox. Show ("this exam is not available ");

28}

29

30 clear all

31 foreach (string item in outForChecking)

32 {

33 submittedTexts. Add (item );

34}

35 outForChecking. Clear ();

Various methods and properties in the Collection class, see aspx "> http://msdn.microsoft.com/zh-cn/library/ms132397.aspx

Related Article

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.