请说明有关"ref"数据类型的信息.以及如何将其用作函数调用和函数定义中的参数?请举例说明. [英] Please explain about the 'ref' datatype. And how it can be used as an argument in function calling and function definition? Please explain with an eg.

查看:677
本文介绍了请说明有关"ref"数据类型的信息.以及如何将其用作函数调用和函数定义中的参数?请举例说明.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请说明有关"ref"数据类型的信息.以及如何将其用作函数调用和函数定义中的参数?请使用eg.

Please explain about the ''ref'' datatype. And how it can be used as an argument in function calling and function definition? Please explain with an eg.

推荐答案

csharp中的Ref数据类型像c ++中的指针一样进行解释.

假设

Ref data type in csharp works like a pointer in c++.

Assume that

public bool functionTest(int a,int b, ref long c)
{
   c=a*b;
}



现在,如果我调用上面的函数



Now if i call the above function

int c;
functionTest(2,3, ref  c)
system.out.print("{0}",c);



它打印出6.

在这里,a,b跟随值机制,c跟随参考机制.如果u更改c值,它将反映所有位置.

有关更多示例,请访问

http://msdn.microsoft.com/en-us/library/0f66670z%28v = vs.71%29.aspx [ ^ ]



it prints 6.

Here a,b are follows pass by value mechanism and c follows pass by reference mechanism. If u change the c value it reflects every where.

For more examples go thru

http://msdn.microsoft.com/en-us/library/0f66670z%28v=vs.71%29.aspx[^]


在通过参数将值传入和传出方法时,可以使用ref.引用要求在传递值之前先对其赋值.
这是一个可以帮助您的链接.
ref和 [
You can use ref when you are passing a value into and out of a method by a parameter. Ref requires values to be assigned before passing them in.
Here is a link that may help you out.
What is the difference between the ref and out keywords in C#?[^]
Good luck.


这篇关于请说明有关"ref"数据类型的信息.以及如何将其用作函数调用和函数定义中的参数?请举例说明.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆