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
9
votes
3 answers

Dictionary TryGetValue with int values, how to avoid double lookup

When doing something like: int value; if (dict.TryGetValue(key, out value)) { if (condition) { //value = 0; this copies by value so it doesn't change the existing value dict[key] = 0; } } else { dict[key] = 0; } Is…
tmakino
  • 618
  • 1
  • 5
  • 20
8
votes
4 answers

Difference between pointer-to-pointer vs reference-to-pointer (C++)

I have a bit of COM code that uses interface pointers. The original author of the code implemented functions that return an interface pointer like this: HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject* &pEnumerator ); // (1) instead of the…
xxbbcc
  • 16,930
  • 5
  • 50
  • 83
8
votes
1 answer

What is the replacement of Out keyword for Async methods in .Net 4.5 and 4.0?

All i want to use Out keyword with my Async function. According to MSDN it is not possible Async modifiers not supports to the out keyword. So is there any alternate in .Net framework 4.5/4.0 ?
Ashish-BeJovial
  • 1,829
  • 3
  • 38
  • 62
8
votes
2 answers

Why would ref be used for array parameters in C#?

I read the page Passing Arrays Using ref and out (C# Programming Guide) and was wondering why we would need to define an array parameter as a ref parameter when it is already a reference type. Won't changes in the callee function be reflected in the…
Tyler Durden
  • 1,188
  • 2
  • 19
  • 35
7
votes
4 answers

C# and VB.Net out parameters

I've got a project in c# which is making use of another project written in vb.net. I am currently able to modify both. I've got a method in the VB project like: Public Sub MethodName(ByVal param1 As String, ByRef param2 As String) param2…
Matthew Grima
  • 1,513
  • 5
  • 25
  • 40
7
votes
2 answers

How to mock an out parameter with It.IsAny<> with Moq?

I would like to verify that a method is only called once. mock.Verify(x => x.Method("String", out It.IsAny()), Times.Once); I don't care about the second parameter, it could be anything. I get the following error message because of…
fota666
  • 141
  • 1
  • 9
7
votes
0 answers

Will ValueTuples make out parameters obsolete?

With the introduction of ValueTuples in C# 7.0 we can now have multiple return values: public (int sum, int count) GetTallies() { return (1, 2); } I'm under the impression that the sole reason for out parameters is to provide a workaround for…
Tim Pohlmann
  • 4,140
  • 3
  • 32
  • 61
7
votes
3 answers

Visual Studio code metrics misreporting lines of code

The code metrics analyser in Visual Studio, as well as the code metrics power tool, report the number of lines of code in the TestMethod method of the following code as 8. At the most, I would expect it to report lines of code as…
Ian Newson
  • 7,679
  • 2
  • 47
  • 80
7
votes
2 answers

How to tell if an out parameter was set already?

Is there a way to know if an out parameter was set already or not. This is the pseudocode for what I am looking for: public virtual string blabla(long num, out bool bval) { if (!bval.HasValue) { //Do some default…
Omtara
  • 2,911
  • 2
  • 19
  • 31
6
votes
3 answers

Why can't I convert from 'out BaseClass' to 'out DerivedClass'?

I just learned that having a generic argument as the type of an out parameter forces that generic type to be invariant. This is surprising to me. I thought out parameters are treated the same as return types (i.e. if the generic parameter is…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
6
votes
1 answer

Use of unassigned local variable error is incorrectly indicated by compiler

Given this code: private void TryIt(Dictionary myDict) { if (myDict?.TryGetValue(1, out int myValue) ?? false) { Console.Out.WriteLine(myValue); // <-- Error CS0165 } } The c# compiler emits: error CS0165: Use of…
6
votes
1 answer

Python NET call C# method which has a return value and an out parameter

I'm having the following static C# method public static bool TryParse (string s, out double result) which I would like to call from Python using the Python NET package. import clr from System import Double r0 = Double.IsNaN(12.3) # works r1, d1 =…
Wollmich
  • 1,616
  • 1
  • 18
  • 46
6
votes
3 answers

Change flash player audio output device

Is there a way to change flash players audio output device? if not, is there a swf player who has this possibility? Thanks!
CatWithGlasses
  • 1,493
  • 2
  • 18
  • 21
6
votes
2 answers

out var _ and out _ difference?

In C# 7 we can do like this: byte.TryParse(p.Value, out _) or like this byte.TryParse(p.Value, out var _) Are there any differences?
AsValeO
  • 2,859
  • 3
  • 27
  • 64
6
votes
7 answers

Clickout in jquery

What is the easiest way to do a clickout in jquery Where a div box hides if clicked anywhere outside it.
Hussein
  • 42,480
  • 25
  • 113
  • 143
1 2
3
39 40