fs[3, 3]
fs[4, 4]
fs[5, 5]
0 10 20 fs[3, 3]
fs[4, 4]
fs[5, 5]
. , . , , , , , , , . , , , , .
. , get set. . , , get set. get set .
:
{ *
get {
//
}
set {
//
}
, int, . , . , set value, , .
, , , , . , , . ( , .)
, My Prop, prop. .
// .
Using System;
class SimpProp {
int prop; // , MyProp
public SimpProp() { prop =0; }
/* prop. . */ public int MyProp { get {
Return prop;
}
set {
if(value >= 0) prop = value;
}
}
}
// , class PropertyDemo { static void Main() {
SimpProp ob = new SimpProp();
Console.WriteLine(" ob.MyProp: " + ob.MyProp);
ob.MyProp = 100; // Console.WriteLine(" ob.MyProp: " + ob.MyProp);
|
|
// prop .
Console.WriteLine(" " +
"-10 ob.MyProp");
ob.MyProp = -10;
Console.WriteLine(" ob.MyProp: " + ob.MyProp);
}
}
.
ob.MyProp: 0 ob.MyProp: 100
-10 ob.MyProp ob.MyProp: 100
. prop , prop. , , . , prop , , .
public, , . , prop, . get prop, set prop , . , , prop. , , .
, , . . , , , get. , , set.
. , (Length). FailSoftArray Length. , , . (, , , .) , Length " ", , FailSoftArray.
// Length FailSoftArray.
Using System;
class FailSoftArray {
int[] a; //
int len; // Length
public bool ErrFlag; //
// , public FailSoftArray(int size) { a = new int[size]; len = size;
}
// Length , public int Length { get {
Return len;
// FailSoftArray. public int this[int index] {
// get. get {
if(ok(index)) {
ErrFlag = false; return a[index];
} else {
ErrFlag = true; return 0;
}
}
// set. set {
if(ok(index)) {
|
|
a[index] = value;
ErrFlag = false;
}
else ErrFlag = true;
}
}
// true, // , private bool ok(int index) {
if(index >= 0 & index < Length) return true; return false;
}
}
// // , class ImprovedFSDemo { static void Main() {
FailSoftArray fs = new FailSoftArray(5); int x;
// Length, for (int i=0; i < fs.Length; i++) fs[i] = i*10;
for (int i=0; i < fs.Length; i++) {
x = f s [ i ];
if(x!= -1) Console.Write(x + " ");
}
Console.WriteLine ();
// fs.Length = 10; // , !
}
}
Length , 1. - get, . , Length , . , .
// fs.Length =10; // , !