C#参数传递

    在 C# 中,既可以通过值也可以通过引用传递参数。通过引用传递参数允许函数成员(方法、属性、索引器、运算符和构造函数)更改参数的值,并保持该更改。若要通过引用传递参数,请使用 ref 或 out 关键字。为简单起见,本主题的示例中只使用了 ref 关键字。有关 ref 和 out 之间的差异的信息,请参见、使用 ref 和 out 传递数组。

一、值传递

    值类型变量直接包含其数据,这与引用类型变量不同,后者包含对其数据的引用。因此,向方法传递值类型变量意味着向方法传递变量的一个副本。方法内发生的对参数的更改对该变量中存储的原始数据无任何影响。如果希望所调用的方法更改参数值,必须使用 ref 或 out 关键字通过引用传递该参数。

    为了简单起见,以下示例使用 ref。下面的示例演示通过值传递值类型参数。通过值将变量 myInt 传递给方法 SquareIt。方法内发生的任何更改对变量的原始值无任何影响。

// PassingParams1.cs 
using System;
class PassingValByVal
...{
    static void SquareIt(int x)
    // The parameter x is passed by value.
    // Changes to x will not affect the original value of myInt.
    ...{
        x *= x;
        Console.WriteLine("The value inside the method: {0}", x);
    }
    public static void Main()
    ...{
        int myInt = 5;
        Console.WriteLine("The value before calling the method: {0}",
           myInt);
        SquareIt(myInt);   // Passing myInt by value.
        Console.WriteLine("The value after calling the method: {0}",
           myInt);
    }
}

输出

The value before calling the method: 5

The value inside the method: 25

The value after calling the method: 5

代码讨论

    变量 myInt 为值类型,包含其数据(值 5)。当调用 SquareIt 时,myInt 的内容被复制到参数 x 中,在方法内将该参数求平方。但在 Main 中,myInt 的值在调用 SquareIt 方法之前和之后是相同的。实际上,方法内发生的更改只影响局部变量 x。

二、引用传递

下面的示例除使用 ref 关键字传递参数以外,其余与“示例 1”相同。参数的值在调用方法后发生更改

// PassingParams2.cs 
using System;
class PassingValByRef
...{
    static void SquareIt(ref int x)
    // The parameter x is passed by reference.
    // Changes to x will affect the original value of myInt.
    ...{
        x *= x;
        Console.WriteLine("The value inside the method: {0}", x);
    }
    public static void Main()
    ...{
        int myInt = 5;
        Console.WriteLine("The value before calling the method: {0}",
           myInt);
        SquareIt(ref myInt);   // Passing myInt by reference.
        Console.WriteLine("The value after calling the method: {0}",
           myInt);
    }
}

输出

The value before calling the method: 5

The value inside the method: 25

The value after calling the method: 25

代码讨论

    本示例中,传递的不是 myInt 的值,而是对 myInt 的引用。参数 x 不是 int 类型,它是对 int 的引用(本例中为对 myInt 的引用)。因此,当在方法内对 x 求平方时,实际被求平方的是 x 所引用的项:myInt。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++和C#中,结构体是一种用户自定义的数据类型,可以包含多个不同类型的成员变量。在函数调用中,通过传递结构体指针作为参数,可以实现对结构体的修改。 在C++中,可以通过使用指针作为函数参数来传递结构体。示例代码如下: ```cpp #include <iostream> struct MyStruct { int num; char ch; }; void ModifyStruct(MyStruct* ptr) { ptr->num = 10; ptr->ch = 'A'; } int main() { MyStruct myStruct; ModifyStruct(&myStruct); std::cout << "Modified struct: " << myStruct.num << ", " << myStruct.ch << std::endl; return 0; } ``` 在上述代码中,定义了一个名为`MyStruct`的结构体,在`ModifyStruct`函数中,通过传递结构体指针`ptr`,可以直接修改结构体的成员变量。 在C#中,结构体是值类型,传递结构体参数时会进行值拷贝。为了实现类似于C++中的指针参数传递,可以使用`ref`关键字。示例代码如下: ```csharp using System; struct MyStruct { public int num; public char ch; } class Program { static void ModifyStruct(ref MyStruct myStruct) { myStruct.num = 10; myStruct.ch = 'A'; } static void Main(string[] args) { MyStruct myStruct; ModifyStruct(ref myStruct); Console.WriteLine("Modified struct: {0}, {1}", myStruct.num, myStruct.ch); } } ``` 在上述代码中,通过`ref`关键字将结构体参数传递给`ModifyStruct`函数,可以直接修改结构体的成员变量。 总结来说,C++和C#都支持通过结构体指针参数传递来修改结构体的值。在C++中,可以直接使用指针作为参数;而在C#中,需要使用`ref`关键字来实现类似的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值