C # Indexer Encapsulation

Source: Internet
Author: User

1. Student Data category

public class Student

{

Public Student ()

{ }

Public Student (string no,string name)

{

This. Studentno = no;

This. Studentname = name;

}

private string Studentno;

private string Studentname;

<summary>

School Number

</summary>

public string Studentno

{

Get

{

return studentno;

}

Set

{

Studentno = value;

}

}

<summary>

name

</summary>

public string Studentname

{

Get

{

return studentname;

}

Set

{

Studentname = value;

}

}

}

2. Arrays

public class MyList

{

<summary>

The actual number of array elements

</summary>

private int count;

student[] Stus = new Student[4];

Number of #region elements

public int Count

{

Get

{

return count;

}

Set

{

Count = value;

}

}

#endregion

#region adding elements

public void Add (Student item)

{

if (Count = = Stus. Length)

{

student[] Newstus = new Student[stus. Length * 2];

Stus. CopyTo (newstus, 0);

Stus = Newstus;

}

Stus[count] = Item;

count++;

}

#endregion

#region Get elements

Public Student Get (int index)

{

return Stus[index];

}

Public Student Get (string name)

{

for (int i = 0; i < count; i++)

{

if (Stus[i]. Studentname = = name)

{

return stus[i];

}

}

return null;

}

#endregion

#region Indexer

Public Student This[int Index]

{

Get

{

return Stus[index];

}

set {Stus[index] = value;}

}

Public Student this[string Name]

{

Get

{

for (int i = 0; i < count; i++)

{

if (Stus[i]. Studentname = = name)

{

return stus[i];

}

}

return null;

}

Set

{

for (int i = 0; i < count; i++)

{

if (Stus[i]. Studentname = = name)

{

Stus[i] = value;

}

}

}

}

#endregion

}

3. Call

static void Main (string[] args)

{

MyList ml = new C0index. MyList ();

ml. ADD (New Student ("001", "AA"));

ml. ADD (New Student ("002", "BB"));

ml. ADD (New Student ("003", "CC"));

ml. ADD (New Student ("004", "DD"));

ml. ADD (New Student ("005", "ee"));

for (int i = 0; i < ml. Count; i++)

{

Console.WriteLine (Ml[i]. Studentname);

}

}

C # Indexer Encapsulation

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.