先上代码:
using System; namespace Welcome { class Program { static void Main(string[] args) { } public static int x; int y; void F(int[] v, int a, ref int b, out int c) { int i = 1; c = a + b++; } } }
在上面申明的变量中,x是静态变量,y是非静态变量(也就是遇到的最多的变量类型),v[0]是数组元素,a是值参数,b是引用参数,c书输出参数,i是局部变量。
静态变量:
带有static修饰符的变量叫做静态变量,一旦静态变量所属的类被装载,直到程序运行结束,变量才消失。
下面一段代码展示ref和out的用法:
using System; namespace Welcome { class Program { /** * ref的作用太明显不过了,不再申请内存空间,多个指针指向同一块地方 * * out的作用不大的感觉 */ static void Main(string[] args) { String str; int i = 1; F(ref i,out str); Console.WriteLine(i+"\n"+str); Console.Read(); } private static void F(ref int i, out String s) { i = i + 1; s = "name"; } } }常量:C#中常量修饰符是const,还可以通过static readonly表示常量,两者的区别是const从编译期开始形成,而static readonly是执行的时候形成。