Length FailSoftArray . , , ErrFlag, . FailSoftArray, Error, ErrFlag, .
// ErrFlag .
Using System;
class FailSoftArray {
int[] a; // int len; //
bool ErrFlag; // ,
//
// , public FailSoftArray(int size) { a = new int[size]; len = size;
}
// Length , public int Length { get {
Return len;
}
}
// Error , public bool Error { get {
Return ErrFlag;
}
}
// 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 FinalFSDemo { static void Main() {
FailSoftArray fs = new FailSoftArray(5);
// Error, for(int i=0; i < fs.Length + 1; i++) {
fs[i] = i * 10; if(fs.Error)
tonsole.WriteLine(" " + i);
}
}
}
Error FailSoftArray. -, ErrFlag , Error, , . -, Error " ". Error , . Main (), , Error.
C# 3.0, _ , , . . :
{ get; set; }
, . , get set , . , , . . .
|
|
, UserCount.
public int UserCount { get; set; }
, . , , . UserCount .
. get set. (.. - ) , private ( " ").
, , . , . , , , . , , . , , .
8, . , . , . 8, . , 8 , , Count Str . .
// .
class MyClass {
11 , public int Count { get; set; } public string Str { get; set; }
}
class ObjlnitDemo { static void Main() {
// MyClass .
MyClass obj =
new MyClass { Count = 100, Str = "" };
Console.WriteLine (obj.Cour^t + " " + obj.Str);
}
}
, Count Str . , 8, :
|
|
8, , LINQ-. .
,
. -, , ref out. -, . , , , , . , get. , . get .
set get , , . , public, set get (public). set get , private. , , , , .
, . , , . private. My Prop, set private.
// .
Using System;
class PropAccess {
int prop; // , MyProp
public PropAccess() { prop = 0; }
/* prop. prop , . */ public int MyProp { get {
Return prop;
}
private set { // prop = value;
}
}
// MyProp. public void IncrPropO {
MyProp++; // . .
}
}
// , class PropAccessDemo { static void Main() {
PropAccess ob = new PropAccess();
Console.WriteLine(" ob.MyProp: " + ob.MyProp);
// ob.MyProp = 100; // ob.IncrProp();
Console.WriteLine(" ob.MyProp : " + ob.MyProp);
}
}
PropAccess set private. , , IncrProp (), PropAccess. ob. My Prop PropAccessDemo .