I noticed that the C # compiler (.NET 4.5.2) does not allow me to compile the following code:
public void Test(out string value) { //value = null; try { value = null; } catch (Exception ex) { //value = null; } }
The following error failed:
The out value parameter must be assigned before the control removes the current method.
But if I uncomment the assignment in the section catch, it compiles successfully.Obviously, it also compiles when I uncomment the assignment to the operator try.
catch
try
So the question is: why is this not enough to initialize the parameter outinside the try block? . Why am I forced to initialize in a block catch?
out
, , try , , , .
, out try, , - try , , , out, , , , , .
catch, ( ), , try- , , , .
, out , . , , value = null;, catch, .
value = null;
, if else, .
if else
MSDN, out ref , , , , ref . , try catch, catch , , , ref.
ref
try catch
, try, finally, , try , , out.
finally
out . try ( , ), -, . catch , / try { ... } catch{ ... }, finally { ... }
try { ... } catch{ ... }
finally { ... }
# Sepcification,
, function member returns ( a return statement through execution reaching the end of the function member body). , - undefined , , , .
function member returns
return statement
through execution reaching the end of the function member body
try..catch Try, catch. , , spec , output parameters .
try..catch
output parameters
- out parameter. Try , .
out parameter
Try
Source: https://habr.com/ru/post/1605989/More articles:Make pdb work with docker - dockerhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1605985/mutable-borrow-seems-to-outlive-its-scope&usg=ALkJrhhawMvhkk68zlWNfOFZH1YERWERLAFailed to install Oracle Developer Tools for Visual Studio - installationMiller-Rabin test: impossible result - javaResize the stargarzer table when type = HTML for ioslides in RStudio - rResizing a star table in Knitr - rHow to call a method using & mut self from another method of the same type? - rustWPF application cannot start and gets a XAMLParseException - c #caffe installation: opencv libpng16.so.16 binding problems - pythonHow do you return a boolean based on the result of a promise? - javascriptAll Articles