c# - Why do we not need to return a value outside of the `using` scope? -
consider code:
public int downloadsoundfile() { using (var x= new x()) { return x.value; } }
and code:
public int downloadsoundfile() { if (x!=null) { return x.value; } }
the first code doesn't give compile time errors in second code error:
not code paths return value
this means should return value outside of if
scope.
why have return value outside of if
scope don't need return value outside of using
scope?
why should return value out of if scope don't need return value out of using scope?
because if
scope might not execute (if condition not satisfied) whereas body of using
scope guaranteed execute (it either return result or throw exception acceptable compiler). if
scope method undefined if condition not satisfied , compiler refuses that.
so should decide value return if condition wrote not satisfied:
public int downloadsoundfile() { if (x != null) { return x.value; } // @ stage should return default value return 0; }
Comments
Post a Comment