I’ve seen some developers use the out keyword on parameter lists of void functions. I’m quite unclear on what the pros and cons are of code below:
List<string> listOfResult;
public void public void (out listOfResult)
{
//bla bla
}
versus
public List<string> c(out listOfResult)
{
List<string> list= new List<string>();
//bla bla
return list;
}
Are these two code snippets perfectly valid or is there any catch around the out keyword?
outkeyword is handy when you need to return more than one value from function. Nice example isTryXXXmethods, which return status of operation instead of throwing exceptions:But I don’t see any reason to use single
outparameter with void methods… Simply return that value from your method. It will be much easier to use. Compare:With
If getting of list could throw exceptions, then you can create method like this: