Questions tagged [out]

In c#, the out keyword causes parameters to be passed by reference from the callee to the caller. The parameter doesn't have to be assigned going into a function but must be assigned before coming out of the function.

The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. For example:

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value = 2;
        Method(out value);
        // value is now 44
    }
}

Although variables passed as out arguments need not be initialized prior to being passed, the called method is required to assign a value before the method returns.

The ref and out keywords are treated differently at run-time, but they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a ref argument and the other takes an out argument.

571 questions
935
votes
27 answers

What's the difference between the 'ref' and 'out' keywords?

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…
TK.
  • 42,559
  • 46
  • 114
  • 145
332
votes
11 answers

Assigning out/ref parameters in Moq

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…
Richard Szalay
  • 78,647
  • 19
  • 169
  • 223
113
votes
8 answers

How to explicitly discard an out argument?

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…
Andrew Ducker
  • 4,806
  • 8
  • 32
  • 46
91
votes
6 answers

Returning two values, Tuple vs 'out' vs 'struct'

Consider a function which returns two values. We can write: // Using out: string MyFunction(string input, out int count) // Using Tuple class: Tuple MyFunction(string input) // Using struct: MyStruct MyFunction(string input) Which…
Xaqron
  • 26,135
  • 39
  • 130
  • 194
55
votes
10 answers

When should I use out parameters?

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,…
kay.one
  • 7,374
  • 6
  • 50
  • 72
43
votes
5 answers

Why can't iterator methods take either 'ref' or 'out' parameters?

I tried this earlier today: public interface IFoo { IEnumerable GetItems_A( ref int somethingElse ); IEnumerable GetItems_B( ref int somethingElse ); } public class Bar : IFoo { public IEnumerable GetItems_A( ref int…
Trap
  • 11,199
  • 15
  • 53
  • 64
42
votes
5 answers

paypal express checkout =>Error: Security header is not valid

Error : Security header is not valid Array ( [TIMESTAMP] => 2014%2d04%2d29T07%3a24%3a29Z [CORRELATIONID] => 6af6749c848d6 [ACK] => Failure [VERSION] => 109%2e0 [BUILD] => 10800277 [L_ERRORCODE0] => 10002 [L_SHORTMESSAGE0]…
Anup GC
  • 724
  • 1
  • 6
  • 16
38
votes
5 answers

Out parameters and exceptions

Say I have the following code: static void Fjuk(out string str) { str = "fjuk!"; throw new Exception(); } static void Main(string[] args) { string s = null; try { Fjuk(out s); …
Niklas
  • 5,026
  • 7
  • 31
  • 42
32
votes
6 answers

Redirect System.out.println

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 ?
EK.
  • 2,550
  • 8
  • 34
  • 48
27
votes
3 answers

How to declare a generic delegate with an out parameter

Func, 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 DetectMethod; }
Benny
  • 7,947
  • 7
  • 53
  • 90
25
votes
6 answers

C# out parameter performance

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…
Tamas Czinege
  • 110,351
  • 39
  • 146
  • 173
21
votes
1 answer

Boxing and unboxing when using out and ref parameters

Does boxing/unboxing occur when a method accepts an out/ref parameter of a ValueType?
brain_pusher
  • 1,427
  • 3
  • 20
  • 31
17
votes
5 answers

Why can't an out parameter have a default value?

Currently when trying to do something in a method that takes an out parameter, I need to assign the value of the out parameter in the method body, e.g. public static void TryDoSomething(int value, out bool itWorkerd) { itWorkerd = true; if…
DaveDev
  • 38,095
  • 68
  • 199
  • 359
17
votes
4 answers

Does C# support inout parameters?

In C#, I am using a StreamReader to read a file, line per line. I am also keeping the current line's number in an int, for reports of possible error messages. Reading each line goes along with some tests (for instance, lines beginning with # are…
Lee White
  • 3,367
  • 7
  • 30
  • 53
16
votes
2 answers

In C# 4.0 why can't an out parameter in a method be covariant?

Given this magical interface: public interface IHat { TRabbit Take(); } And this class hierarchy: public class Rabbit { } public class WhiteRabbit : Rabbit { } I can now compile this: IHat hat1 = null; IHat
Daniel Earwicker
  • 108,589
  • 35
  • 194
  • 274
1
2 3
38 39