Learn C # (attribute 2) a week

zhaozj2021-02-16  48

Search C # (attribute 2) C # Talent Bird (QQ: 249178521)

5. SET statement

· SET statement

W is assigned through the Value identifier.

w can contain any statement (even no statement)

Struct Time

{

...

Public int hour

{

...

SET {

IF (Value <0 || Value> 24)

Throw New ArgumentException ("Value");

Hour = Value;

}

}

Private Int Hour, Minute, Second;

}

Time Lunch = New Time ();

...

Lunch.Hour = 12;

When writing an attribute, the attribute's SET statement is automatically run.

In the above example, the TIME structure class has a integer attribute HOUR, so the value assigned to this property must be a integer value. E.g:

Lunch.Hour = 12;

Assign a integer 12 to the HOUR attribute of Lunch, this statement automatically calls the set statement of the property. The SET statement is assigned by the Value identifier. For example, if 12 is assigned to the HOUR attribute, the value of VAUE is 12. Note that Value is not a keyword. Value is just an identifier in a set statement. You can declare value for a variable in any statement outside the SET statement. E.g:

Public int hour

{

Get {int value; ...} // correct

Set {Int value; ...} // Error

}

6. Read-only properties

l The read-only attribute is only a get statement

Ø Any write operation will cause errors

Ø Just like a read-only field

Struct Time

{

...

Public int hour

{

get

{

Return Hour;

}

}

Private Int Hour, Minute, Second;

}

Time Lunch = New Time ();

...

Lunch.Hour = 12; // Error

...

Lunch.Hour = 2; // Error

A property can not have to declare the get statement and set statements at the same time. You can only declare a get statement. In this case, the attribute is read-only, and any write operation will cause an error. For example, the following statement will cause an error:

Lunch.Hour = 12;

Because Hour is read-only attribute.

However, it should be noted that attributes must include at least one GET or SET statement, and an attribute cannot be empty:

Public int hour {} // error

7. Write only attributes

l You only write the property only SET statement

Ø Any read operation is wrong

Struct Time

{

...

Public int hour

{

SET {

IF (Value <0 || Value> 24)

Throw New OutofrangeException ("Hour");

Hour = Value;

}

}

Private Int Hour, Minute, Second;

}

Time Lunch = New Time ();

...

Console.writeline (Lunch. Hour); // Error

...

Lunch.Hour = 12; // Error

A property can not have to declare the get statement and set statements at the same time. You can only declare a SET statement. In this case, the attribute is only written, and any read operation will cause an error. For example, the following statement will cause an error: console.writeline (Lunch. Hour);

Because Hour is only write properties.

And the following examples look like:

Lunch.Hour = 2;

The actual operation of this statement is this:

Lunch.Hour = Lunch.Hour 2;

It performs a read operation, so it is wrong. Therefore, such a composite assignment operator cannot be used for read-only properties, and cannot be used to write only attributes.

转载请注明原文地址:https://www.9cbs.com/read-22153.html

New Post(0)