Const修饰,初始化一次,无法改变
public class constClass{
public const string constVa= "this is a const";
public constClass()
{ constVa = "this is not a const" //编译错误}
}
也可以在函数中声明const
public void constMethod()
{
const string constVa = "this is a fixed string";
constVa = "this is a testing" //编译错误
}
readonly 修饰符
同const,唯一区别在于可以在构造函数中初始化(别的不可以),另一个区别,const是默认static,访问使用constClass.constVa,但readonly不是的,需要另加static修饰,如public static readonly int readandstaticVa = 111;
partial修饰符 - 可以在多个cs文件中使用的变量,只能修饰在class或者struct上面,可以用来分下很大的类
如
employee.internal.cs
partial class employee{
...
}
employee.cs
public class employee{
}