Click here to Skip to main content
15,867,488 members
Home / Discussions / C#
   

C#

 
Question'out' keyword, to use or not to use Pin
Foothill19-Apr-16 8:35
professionalFoothill19-Apr-16 8:35 
AnswerRe: 'out' keyword, to use or not to use Pin
clapclap19-Apr-16 9:05
clapclap19-Apr-16 9:05 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill19-Apr-16 9:24
professionalFoothill19-Apr-16 9:24 
GeneralRe: 'out' keyword, to use or not to use Pin
Sascha Lefèvre19-Apr-16 9:29
professionalSascha Lefèvre19-Apr-16 9:29 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill19-Apr-16 9:51
professionalFoothill19-Apr-16 9:51 
GeneralRe: 'out' keyword, to use or not to use Pin
F-ES Sitecore21-Apr-16 0:26
professionalF-ES Sitecore21-Apr-16 0:26 
AnswerRe: 'out' keyword, to use or not to use Pin
Sander Rossel19-Apr-16 11:08
professionalSander Rossel19-Apr-16 11:08 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill19-Apr-16 11:38
professionalFoothill19-Apr-16 11:38 
I see your point when you run comparisons against the functional programming paradigm. In the question, I should have expanded to include that the function's success/failure result was outside of exception handling. The function should, by all means, throw exceptions but the usage of out really does seem best fitted to TryParse style functions where I want to know if the function did what it was supposed to do before I try to do anything with the result. With TryParse, it almost seems like it was designed to swallow certain exceptions and return false if they occur.

Let me see if I can give a better example. Let's go with an web environment that supports both internal and external user logon.
C#
using System.DirectoryServices.AccountManagement;

public static class AuthorizationManager
{
 public static bool UserHasAccess(string userName, out UserPricipal user)
 {
  // function runs a check against Active Directory for membership of a user group
  // if the specified user is a member, pass back the user's info to use elsewhere (logging? other authorization functions?)
 }
}

The function basically answers two questions, is the user an internal user by populating the user parameter and that the user has the required access.

I am seeing similar approaches to this in the black box code of the Cryptography namespace. Like with RandomNumberGenerator.GetBytes(byte[]) where you pass a instantiated array of bytes to the function and it populates all the elements with random values. Why is this method used instead of byte[] rndAry = rand.GetBytes(n);? The deeper I delve into C# and .Net, the more contradictions I find.
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

GeneralRe: 'out' keyword, to use or not to use Pin
Sander Rossel19-Apr-16 12:24
professionalSander Rossel19-Apr-16 12:24 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill19-Apr-16 14:06
professionalFoothill19-Apr-16 14:06 
GeneralRe: 'out' keyword, to use or not to use Pin
Sander Rossel19-Apr-16 21:25
professionalSander Rossel19-Apr-16 21:25 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill20-Apr-16 5:14
professionalFoothill20-Apr-16 5:14 
GeneralRe: 'out' keyword, to use or not to use Pin
Brisingr Aerowing20-Apr-16 4:23
professionalBrisingr Aerowing20-Apr-16 4:23 
GeneralRe: 'out' keyword, to use or not to use Pin
F-ES Sitecore21-Apr-16 0:33
professionalF-ES Sitecore21-Apr-16 0:33 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill21-Apr-16 4:39
professionalFoothill21-Apr-16 4:39 
GeneralRe: 'out' keyword, to use or not to use Pin
Richard MacCutchan19-Apr-16 20:46
mveRichard MacCutchan19-Apr-16 20:46 
GeneralRe: 'out' keyword, to use or not to use Pin
Sander Rossel19-Apr-16 21:26
professionalSander Rossel19-Apr-16 21:26 
GeneralRe: 'out' keyword, to use or not to use Pin
Mycroft Holmes19-Apr-16 21:54
professionalMycroft Holmes19-Apr-16 21:54 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill20-Apr-16 5:03
professionalFoothill20-Apr-16 5:03 
GeneralRe: 'out' keyword, to use or not to use Pin
Sander Rossel20-Apr-16 5:59
professionalSander Rossel20-Apr-16 5:59 
AnswerRe: 'out' keyword, to use or not to use Pin
Gerry Schmitz19-Apr-16 12:36
mveGerry Schmitz19-Apr-16 12:36 
AnswerRe: 'out' keyword, to use or not to use Pin
Bernhard Hiller19-Apr-16 21:25
Bernhard Hiller19-Apr-16 21:25 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill20-Apr-16 3:16
professionalFoothill20-Apr-16 3:16 
AnswerRe: 'out' keyword, to use or not to use Pin
V.19-Apr-16 22:04
professionalV.19-Apr-16 22:04 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill20-Apr-16 3:13
professionalFoothill20-Apr-16 3:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.