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.

598 questions
16
votes
5 answers

println method - what do the last 2 letters (l & n) stand for?

I guess it's related to println()'s newline functionality ('\n'), but in abbreviated letter-based form, that would be nl rather than ln. Thank you for any comments.
user2911290
  • 1,396
  • 1
  • 16
  • 26
15
votes
2 answers

mysql "not a variable or NEW pseudo-variable" message

I'm trying to create a procedure that will enter data and then return a message in the OUT parameter, however i'm getting this message "argument 5 for routine hospital.alextest10 is not a variable or NEW pseudo-variable in BEFORE trigger" i have…
alexei7
  • 182
  • 1
  • 3
  • 11
15
votes
4 answers

Using a variable as an out argument at point of declaration

When reading a comment to an answer I saw the following construct to declare and initialize a variable: int variable = int.TryParse(stringValue, out variable) ? variable : 0; Is this allowed, correct and well defined in C#? What happens under the…
dalle
  • 18,057
  • 5
  • 57
  • 81
15
votes
3 answers

How can I indent cout output?

I'm trying to print binary tree void print_tree(Node * root,int level ) { if (root!=NULL) { cout<< root->value << endl; } //... } How can I indent output in order to indent each value with level '-' chars.
Fantomas
  • 1,495
  • 4
  • 12
  • 21
13
votes
4 answers

Why do C# out generic type parameters violate covariance?

I'm unclear as to why the following code snippet isn't covarient? public interface IResourceColl : IEnumerable where T : IResource { int Count { get; } T this[int index] { get; } bool TryGetValue( string SUID, out T obj );…
MerickOWA
  • 7,453
  • 1
  • 35
  • 56
13
votes
2 answers

How to call a .net method with an output parameter?

I just want to call the GenerateScript method of Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator from PowerShell. #C public void GenerateScript( IScriptFragment scriptFragment, out string script ) I found this, but I do not get…
bernd_k
  • 11,558
  • 7
  • 45
  • 64
13
votes
2 answers

How to mark function argument as output

C# allows to mark function argument as output only: void func(out int i) { i = 44; } Is it possible to do something similar in C/C++? This could improve optimization. Additionally is should silence out warnings "error: 'myVar' may be used…
Daniel Frużyński
  • 2,091
  • 19
  • 28
12
votes
3 answers

Out parameter might not be initialized before accessing

Why is the code below private static List MergeDatasetsListBranch(out List datasetsList) { if(datasetsList == null) datasetsList=new List(); datasetsList=new…
Tarec
  • 3,268
  • 4
  • 30
  • 47
11
votes
3 answers

Null-coalescing out parameter gives unexpected warning

Using this construct: var dict = new Dictionary(); var result = (dict?.TryGetValue(1, out var value) ?? false) ? value : "Default"; I get an error saying CS0165 use of unassigned local variable 'value' which is not what I expect. How…
Jakob Möllås
  • 4,239
  • 3
  • 33
  • 61
11
votes
5 answers

C# out parameters vs returns

So I am new to C# and I am having difficulty understanding out. As opposed to just returning something from a function using System; class ReturnTest { static double CalculateArea() { double r=5; double area = r * r *…
Frank Visaggio
  • 3,642
  • 9
  • 34
  • 71
10
votes
5 answers

Why am I getting these out parameter errors in C#?

I am new to C#. I've tried this with out parameter in C# using System; using System.Collections.Generic; using System.Linq; using System.Text; class First { public void fun(out int m) { m *= 10; Console.WriteLine("value of…
Sachin Kumar
  • 781
  • 1
  • 9
  • 28
9
votes
2 answers

Does specifying the OutAttribute on ByRef internal methods currently do anything?

VB.NET doesn't have out parameters, but you can specify ByRef on COM and P/Invoke methods to get the same effect for external methods. Does specifying the same on internal methods (i.e. methods only called by .NET code) actually help the…
Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
9
votes
1 answer

Moq callback with out parameter

I'm trying to use Moq to mock a callback for the following method signature: ResponseHeader AddIncentives( Hs_transRow[] data, out ResponseBody responseBody); I want my callback to use the data which is passed in. However, I'm…
Tim
  • 5,435
  • 7
  • 42
  • 62
9
votes
2 answers

C# PInvoke out strings declaration

In C# PInvoke, how do I pass a string buffer so that the C DLL fills it and returns? What will be the PInvoke declaration? The C function declaration is int GetData(char* data, int buflength); In C#, I have declared it…
akif
  • 12,034
  • 24
  • 73
  • 85
9
votes
7 answers

Using REF & OUT keywords with Passing by Reference & Passing by Value in C#

Here is what I understand so far: PASS BY VALUE Passing by value means a copy of an argument is passed. Changes to that copy do not change the original. PASS BY REFERENCE Passing by reference means a reference to the original is passed. changes to…
Goober
  • 13,146
  • 50
  • 126
  • 195
1
2
3
39 40