想要使用void方法返回一个值 [英] want to return a value using void method

查看:47
本文介绍了想要使用void方法返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只使用void方法返回一个值..

i want to return a value using only void method ..

推荐答案

这会对你有帮助....



从无效函数中返回值 [ ^ ]
This will help you....

Returning value from Void Function[^]


void 不返回值的方法,但是您可以使用 ref 或<$ c $返回更改的值c> out :

void methods to not return values, however you can return changed values with ref or out:
void method1(string p1, ref int i)
{
   i = 10; // will be changed for the caller 
}

void method2(string p1, out int j)
{
   j = 20; // you must set an 'out' parameter or the compiler will complain
}



http://www.dotnetperls.com/out [ ^ ]

http://www.dotnetperls.com/ref [ ^ ]


我担心空洞无法返回任何东西,因此它们被称为'空洞' - 'void'是一个奇特的词,意思是什么都没有'或'空'英文。



您可以随时尝试使用 out ,如下所示:

I'm afraid that voids cannot return anything, hence why they are called 'voids' - 'void' is a fancy word that means 'nothing' or 'empty' in English.

You can always try using an out, like this:
string test = "Starting Value"
Console.WriteLine(test); //output: Starting Value
Modify(out test);
Console.WriteLine(test); //output: Modified Value





void 修改的定义如下:



And the void Modify is defined like this:

void Modify(out string input)
{
   input = "Modified Value";
}


这篇关于想要使用void方法返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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