值类型和引用类型参数

值类型和引用类型参数

1、             值类型和引用类型

当使用值类型作为参数,传递的是参数的值的拷贝,调用的方法不会改变原来的参数。当使用引用类型作为参数时,传递的是引用的拷贝,调用的方法得到指向同一数据的指针。

      范例:

class SomeClass

{

    public int ChangeInt(int val)

    {

        return val*2;

    }

}

class ValRefTest

{

    static void Main(string[] args)

    {

        SomeClass sc = new SomeClass();

        int val1 = 3;

        int val2 = sc.ChangeInt(val1);

        Console.WriteLine("val1 = {0}, val2 = {1}", 

            val1, val2);

    }

}

输出结果为:var1=3,var2=6

class AnotherClass

{

    public int ID;

}

class SomeClass

{

    public AnotherClass ChangeObject(AnotherClass ref1)

    {

        ref1.ID = ref1.ID*2;

        return ref1;

    }

}

class ValRefTest

{

    static void Main(string[] args)

    {

      SomeClass sc = new SomeClass();  

AnotherClass ref1 = new AnotherClass();

        ref1.ID = 3;

        AnotherClass ref2 = sc.ChangeObject(ref1);

        Console.WriteLine("ref1.ID = {0}, ref2.ID = {1}",

            ref1.ID, ref2.ID);

    }

}

输出结果:ref1.ID=6,ref2.ID=6
  
  
 
  
  

2、             Ref

使用Ref,则传递参数前必须初始化,代码如下:

class SomeClass

{

    public void ChangeInt(ref int val)

    {

        var = val*2;

    }

}

class ValRefTest

{

    static void Main(string[] args)

    {

        SomeClass sc = new SomeClass();

        int val1 = 3;

        sc.ChangeInt(ref val1);

        Console.WriteLine("val1 = {0}", val1);

    }

}

输出结果为:var1=6

 

3、             Out

使用Out,必须在该方法中对参数进行赋值,而不必在传递参数前进行初始化。

class SomeClass

{

    public void ChangeInt(out int val)

    {

        var = 2;

    }

}

class ValRefTest

{

    static void Main(string[] args)

    {

        SomeClass sc = new SomeClass();

        int val1 ;

        sc.ChangeInt(out val1);

        Console.WriteLine("val1 = {0}", val1);

    }

}

输出结果为:var1=2

4、             Ref和引用类型

使用Ref来传递引用类型和使用Ref来传递值类型是一样。

class AnotherClass
   
   
{
   
   
    public int ID;
   
   
}
   
   
class SomeClass
   
   
{
   
   
//      public AnotherClass ChangeObject(AnotherClass ref1)
   
   
    public AnotherClass ChangeObject(ref AnotherClass ref1)
   
   
    {
   
   
        ref1.ID = ref1.ID*2;
   
   
        return ref1;
   
   
    }
   
   
}
   
   
 
   
   
class ValRefTest
   
   
{
   
   
    static void Main(string[] args)
   
   
    {
   
   
        SomeClass sc = new SomeClass();
   
   
        AnotherClass ref1 = new AnotherClass();
   
   
        ref1.ID = 3;
   
   
//          AnotherClass ref2 = sc.ChangeObject(ref1);
   
   
        AnotherClass ref2 = sc.ChangeObject(ref ref1);
   
   
        Console.WriteLine("ref1.ID = {0}, ref2.ID = {1}",
   
   
            ref1.ID, ref2.ID);
   
   
    }
   
   
}
   
   

5、             Out和引用类型

使用Out传递引用类型,在调用的方法内部必须对参数赋值,注意不是对参数的字段赋值。

class AnotherClass
   
   
{
   
   
    public int ID;
   
   
}
   
   
class SomeClass
   
   
{
   
   
    // This won't compile
   
   
    public AnotherClass ChangeObject(out AnotherClass ref1)
   
   
    {
   
   
        ref1.ID = ref1.ID*2;
   
   
        return ref1;
   
   
    }
   
   
    // This won't compile
   
   
    public AnotherClass ChangeObject(out AnotherClass ref1)
   
   
    {
   
   
        ref1.ID = 4;
   
   
        return ref1;
   
   
    }
   
   
    // This won't compile
   
   
    public AnotherClass ChangeObject(out AnotherClass ref1)
   
   
    {
   
   
        int x = ref1.ID;
   
   
        ref1 = new AnotherClass();
   
   
        ref1.ID = x * 2;
   
   
        return ref1;
   
   
    }
   
   
 
   
   
    // This WILL compile
   
   
    public AnotherClass ChangeObject(out AnotherClass ref1)
   
   
    {
   
   
        ref1 = new AnotherClass();
   
   
        ref1.ID = 99;
   
   
        return ref1;
   
   
    }
   
   
}
   
   
 
   
   
class RefByOutTest
   
   
{
   
   
    static void Main(string[] args)
   
   
    {
   
   
        SomeClass sc = new SomeClass();
   
   
        AnotherClass ref1 = new AnotherClass();
   
   
        ref1.ID = 3;
   
   
 
   
   
        AnotherClass ref2 = sc.ChangeObject(out ref1);
   
   
        Console.WriteLine("ref1.ID = {0}, ref2.ID = {1}",
   
   
            ref1.ID, ref2.ID);
   
   
 
   
   
    }
   
   
}
   
   

输出结果为:ref1.ID = 99, ref2.ID = 99

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值