C#中的类的默认访问权限是什么?
如果未指定访问修饰符,则默认值为内部。内部访问说明符允许类将其成员变量和成员函数公开给当前程序集中的其他函数和对象。换句话说,任何具有内部访问说明符的成员都可以从定义该成员的应用程序内定义的任何类或方法中进行访问。
以下是显示内部访问说明符用法的示例-
示例
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