Demonstrate Index indicator usage (C #)

Source: Internet
Author: User
Tags comments constructor
// Frog recommendation: demonstrate Index indicator usage (C #)
// This example shows the number of vertices in a continuous natural number. In addition to 1, the prime number can be divisible by 1 and cannot be divisible by others.
// Sorry, when I was a child, I was not familiar with mathematics. I didn't understand how to judge the prime number algorithm. However, this does not affect the creation and use of index indicators. Who knows, please give me some advice.
// There are many comments. If you delete all comments, the code is still very compact and there is no way. Now you have to do some basic exercises.
Using System; // import the namespace
Class BitArray // defines a class BitArray
{
Int [] bits; // defines an int array.
Int length; // defines an int variable. Note: variables without modifiers are private by default.
Public BitArray (int length) {// defines the constructor of this class. In C #, the constructor with the same name of the class is used as the constructor, and the new keyword is defined in VB. NET.
If (length <0) throw new ArgumentException (); // if the length is smaller than 0, an ArgumentException exception is thrown.
Bits = new int [(length-1)> 5) + 1]; // assign a value to the bits array, and subtract 1 from the length to the right, shift 5, and add 1.
This. length = length; // use the length parameter to assign values to the length of the private variable. Pay attention to the usage of the this keyword. this indicates the class being referenced, that is, the class BitArray. this has other usage.
}
Public int Length {// set a read-only attribute,
Get {return length;} // the property returns the length value of the private variable, which is case sensitive.
}
Public bool this [int index] {// Build a read/write index indicator
Get {// Set the get indicator
If (index <0 | index> = length) {// if the passed index value is smaller than 0 or greater than the array boundary, an IndexOutOfRangeException exception is thrown.
Throw new IndexOutOfRangeException ();
}
Return (bits [index> 5] & 1 <index )! = 0; //, if it is a prime number, 1 is returned. If it is not a prime number, 1 is returned. Do not ask me the relationship between this bit algorithm and the prime number, I don't know the underlying things such as bit and assembly.
}
Set {// set the set indicator
If (index <0 | index> = length) {// check whether the input index is within the proper range, just like the get access, in actual programming, it should also be the responsibility to handle these potential errors, which can enhance the robustness of the program.
Throw new IndexOutOfRangeException ();
}
If (value ){
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.