out

C# out parameter performance

瘦欲@ 提交于 2019-11-27 20:57:03
问题 Do out parameters in C# have any performance implications I should know about? (Like exceptions) I mean, is it a good idea to have a method with an out parameter in a loop that will run a couple of million times a second? I know it's ugly but I am using it the same way as Int32.TryParse is using them - returning a bool to tell if some validation was successful and having an out parameter containing some additional data if it was successful. 回答1: I doubt that you'll find any significant

When should I use out parameters?

不想你离开。 提交于 2019-11-27 19:35:39
I don't understand when an output parameter should be used, I personally wrap the result in a new type if I need to return more than one type, I find that a lot easier to work with than out. I have seen method like this, public void Do(int arg1, int arg2, out int result) are there any cases where that actually makes sense? how about TryParse , why not return a ParseResult type? or in the newer framework return a null-able type? Out is good when you have a TryNNN function and it's clear that the out-parameter will always be set even if the function does not succeed. This allows you rely on the

How to declare a generic delegate with an out parameter [duplicate]

耗尽温柔 提交于 2019-11-27 02:42:11
问题 This question already has an answer here: Func<T> with out parameter 4 answers Func<a, out b, bool> , just don't compile, how to declare that i want the second parameter be an out one? I want to use it like this: public class Foo() { public Func<a, out b, bool> DetectMethod; } 回答1: Actually, Func is just a simple delegate declared in the .NET Framework. Actually, there are several Func delegates declared there: delegate TResult Func<TResult>() delegate TResult Func<T, TResult>(T obj) delegate

How to explicitly discard an out argument?

你离开我真会死。 提交于 2019-11-27 02:28:35
问题 I'm making a call: myResult = MakeMyCall(inputParams, out messages); but I don't actually care about the messages. If it was an input parameter I didn't care about I'd just pass in a null. If it was the return I didn't care about I'd just leave it off. Is there a way to do something similar with an out, or do I need to declare a variable that I will then ignore? 回答1: Starting with C# 7.0, it is possible to avoid predeclaring out parameters as well as ignoring them. public void

When should I use out parameters?

北战南征 提交于 2019-11-26 19:55:35
问题 I don't understand when an output parameter should be used, I personally wrap the result in a new type if I need to return more than one type, I find that a lot easier to work with than out. I have seen method like this, public void Do(int arg1, int arg2, out int result) are there any cases where that actually makes sense? how about TryParse , why not return a ParseResult type? or in the newer framework return a null-able type? 回答1: Out is good when you have a TryNNN function and it's clear

Redirect System.out.println

时光毁灭记忆、已成空白 提交于 2019-11-26 16:44:44
My application has many System.out.println() statements. I want to catch messages from println and send them to the standard logger (Log4j, JUL etc). How to do that ? paxdiablo The System class has a setOut and setErr that can be used to change the output stream to, for example, a new PrintStream with a backing File or, in this case, probably another stream which uses your logging subsystem of choice. Keep in mind you may well get yourself into trouble if you ever configure your logging library to output to standard output or error (of the infinite recursion type, possibly). If that's the case

Redirect System.out.println

两盒软妹~` 提交于 2019-11-26 04:27:49
问题 My application has many System.out.println() statements. I want to catch messages from println and send them to the standard logger (Log4j, JUL etc). How to do that ? 回答1: The System class has a setOut and setErr that can be used to change the output stream to, for example, a new PrintStream with a backing File or, in this case, probably another stream which uses your logging subsystem of choice. Keep in mind you may well get yourself into trouble if you ever configure your logging library to

Assigning out/ref parameters in Moq

耗尽温柔 提交于 2019-11-26 00:53:58
问题 Is it possible to assign an out / ref parameter using Moq (3.0+)? I\'ve looked at using Callback() , but Action<> does not support ref parameters because it\'s based on generics. I\'d also preferably like to put a constraint ( It.Is ) on the input of the ref parameter, though I can do that in the callback. I know that Rhino Mocks supports this functionality, but the project I\'m working on is already using Moq. 回答1: Moq version 4.8 (or later) has much improved support for by-ref parameters:

What&#39;s the difference between the &#39;ref&#39; and &#39;out&#39; keywords?

半腔热情 提交于 2019-11-25 23:28:41
问题 I\'m creating a function where I need to pass an object so that it can be modified by the function. What is the difference between: public void myFunction(ref MyClass someClass) and public void myFunction(out MyClass someClass) Which should I use and why? 回答1: ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function. So while ref is two-ways, out is out-only. 回答2: The ref modifier