c语言ref用法,C#ref是像C/C++中的指针还是C中的引用?

在C#中,当你看到一个引用类型的东西(即用类而不是struct声明的类型)时,你基本上总是通过一个指针来处理对象。在C中,默认情况下,一切都是一个值类型,而在C#中,默认情况下,一切都是一个引用类型。

当你在C#参数列表中说“ref”时,你真正说的更像是一个“指针指针”。你说的是,在方法中,你要替换的不是对象的内容,而是对对象本身的引用,在代码中调用你的方法。

除非这是你的意图,那么你应该直接传递引用类型;在C#中,传递引用类型很便宜(类似于在C中传递引用)。

学习/了解C#中的值类型和引用类型之间的差异。它们是该语言中的一个主要概念,如果您尝试在C#领域中使用C对象模型,则事情会变得非常混乱。

以下是基本上语义相当的程序:

#include

class AClass

{

int anInteger;

public:

AClass(int integer)

: anInteger(integer)

{ }

int GetInteger() const

{

return anInteger;

}

void SetInteger(int toSet)

{

anInteger = toSet;

}

};

struct StaticFunctions

{

// C# doesn't have free functions, so I'll do similar in C++

// Note that in real code you'd use a free function for this.

static void FunctionTakingAReference(AClass *item)

{

item->SetInteger(4);

}

static void FunctionTakingAReferenceToAReference(AClass **item)

{

*item = new AClass(1729);

}

};

int main()

{

AClass* instanceOne = new AClass(6);

StaticFunctions::FunctionTakingAReference(instanceOne);

std::cout << instanceOne->GetInteger() << "\n";

AClass* instanceTwo;

StaticFunctions::FunctionTakingAReferenceToAReference(&instanceTwo);

// Note that operator& behaves similar to the C# keyword "ref" at the call site.

std::cout << instanceTwo->GetInteger() << "\n";

// (Of course in real C++ you're using std::shared_ptr and std::unique_ptr instead,

// right? :) )

delete instanceOne;

delete instanceTwo;

}

而对于C#:

using System;

internal class AClass

{

public AClass(int integer)

: Integer(integer)

{ }

int Integer { get; set; }

}

internal static class StaticFunctions

{

public static void FunctionTakingAReference(AClass item)

{

item.Integer = 4;

}

public static void FunctionTakingAReferenceToAReference(ref AClass item)

{

item = new AClass(1729);

}

}

public static class Program

{

public static void main()

{

AClass instanceOne = new AClass(6);

StaticFunctions.FunctionTakingAReference(instanceOne);

Console.WriteLine(instanceOne.Integer);

AClass instanceTwo = new AClass(1234); // C# forces me to assign this before

// it can be passed. Use "out" instead of

// "ref" and that requirement goes away.

StaticFunctions.FunctionTakingAReferenceToAReference(ref instanceTwo);

Console.WriteLine(instanceTwo.Integer);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值