C#中类的内部变量的作用域是什么?
使用内部访问说明符设置内部变量。
internal double length; internal double width;
具有内部访问说明符的任何成员都可以从定义该成员的应用程序内定义的任何类或方法中进行访问。
示例
using System;
namespace RectangleApplication {
class Rectangle {
//成员变量
internal double length;
internal double width;
double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
} //end class Rectangle
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}输出结果
Length: 4.5 Width: 3.5 Area: 15.75