C#中类的成员

readonly:数据成员中字段只读形式(对于只读字段只能在字段的定义中和它的所属类的构造函数中进行修改)

举例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace program
{
    public class a
    {
        public static readonly int x = 1;    //只读数据
    }
}

namespace program1
{
    class b
    {
        static void Main()
        {
            Console.WriteLine("x的值={0}", program.a.x);
            Console.ReadLine();
        }
    }
}

static:使用了static修饰符的方法是静态方法,它不属于类的某一个实例。非静态方法可以访问类中的任何成员,而静态方法只能访问类中的静态成员。

方法的使用举例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class test
{
    int x;
    static int y;
    void k()       //非静态方法可以访问任何成员
    {
        x = 1;   
        y = 1;
    }
    static void w()  //静态方法只能访问静态成员
    {
 //       x = 1;   //静态方法不可以访问非静态成员
        y = 1;
    }
    static void Main()
    {
        test t = new test();
        t.x = 1;
//        t.y = 1;       //不能再类的实例中访问静态成员
//        test.x = 1;    //不能使用类名访问非静态成员
        test.y = 1;    //可以使用类名访问静态成员
    }
}

ref:引用型参数参数向方法传递形参时,编译程序将把实际值在内存中的地址传递给方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class reftest
{
    public static void test(ref int ival)
    {
        ival += 2;
    }
    public static void Main()
    {
        int i = 3;   //变量需要初始化
        test(ref i);
        Console.WriteLine(i);
        Console.ReadLine();
    }
}

out:输出参数用于传递方法返回的数据,与引用型参数类似,输出型函数也不开辟新的内存区域

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class reftest
{
    public static void test(ref int ival)
    {
        ival += 2;
    }
    public static void Main()
    {
        int i = 3;   //变量需要初始化
        test(ref i);
        Console.WriteLine(i);
        Console.ReadLine();
    }
}

params:如果形参表中包含了数组型的参数,那么它必须在参数表中位于最后,另外数组型参数只允许一维数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class test
{
    static void f(params int [] args)
    {
        Console.WriteLine("#params:{0}", args.Length);
        for (int i = 0; i < args.Length; i++)
            Console.WriteLine("\t args[{0}] = {1}", i, args[i]);
    }
    static void Main()
    {
        f();
        f(1);
        f(1, 2);
        f(1, 2, 3);
        f(new int[] { 1, 2, 3, 4 });
        Console.ReadLine();
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值