Generic series: use generic to create a read-only set

Source: Internet
Author: User

4.9 create a read-only set Using Generics

Problem

You want the information in a set in the class to be accessed by the outside world, but you do not want the user to change the set.

Solution

Using ReadOnlyCollection <T> to package, you can easily implement read-only collection classes. For example, the Lottery class contains a winning number, which can be accessed but cannot be changed:

Public class Lottery

{

// Create a list.

List <int> _ numbers = null;

Public Lottery ()

{

// Initialize the internal list

_ Numbers = new List <int> (5 );

// Add Value

_ Numbers. Add (17 );

_ Numbers. Add (21 );

_ Numbers. Add (32 );

_ Numbers. Add (44 );

_ Numbers. Add (58 );

}

Public ReadOnlyCollection <int> Results

{

// Return the result of a package.

Get {return new ReadOnlyCollection <int> (_ numbers );}

}

}

Lottery has an internal List <int>, which contains the winning number entered in the constructor. The interesting part is that it has a public attribute called Results. The returned ReadOnlyCollection <int> type shows the winning number, so that users can use it through the returned instance.

If you try to set a value in the Set, a compilation error is thrown:

Lottery tryYourLuck = new Lottery ();

// Print the result.

For (int I = 0; I <tryYourLuck. Results. Count; I ++)

{

Console. WriteLine ("Lottery Number" + I + "is" + tryYourLuck. Results [I]);

}

// Change the winning number!

TryYourLuck. Results [0] = 29;

// The Last Row triggers an Error: // Error 26 // Property or indexer

// 'System. Collections. ObjectModel. ReadOnlyCollection <int>. this [int]'

// Cannot be assigned to -- it is read only

Discussion

The main advantage of ReadOnlyCollection is its flexibility. It can be used as an interface in any collection that supports IList or IList <T>. ReadOnlyCollection can also Wrap general arrays like this:

Int [] items = new int [3];

Items [0] = 0;

Items [1] = 1;

Items [2] = 2;

New ReadOnlyCollection <int> (items );

This provides a way to standardize the read-only attributes of a class, and makes the class library user accustomed to this simple read-only attribute return type.

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.