'The out parameter does not work in C#. Why?

The out parameter does not work in C#. Why?

Here is my code:

using System;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        var hashSet = new HashSet<int>(2);
        Console.WriteLine(hashSet.Contains(2));
    }
    
    public ListNode ReverseList(ListNode head) 
    {
        helper(head, out var newHead);
        return newHead;
    }
    
    private ListNode helper(ListNode current, out ListNode newHead) 
    {
        newHead = null;
        
        if (current == null) 
        {
            return null;
        }
        
        var next = helper(current.next, out newHead);

        if (next == null) 
        {
            newHead = current;
        } 
        else 
        {
            next.next = current;
        }
        
        return current;
    }
}

public class ListNode 
{
    public int val;
    public ListNode next;

    public ListNode(int val = 0, ListNode next = null) 
    {
        this.val = val;
        this.next = next;
    }
}

Here is the error I am getting:

Compilation error (line 13, col 30): ) expected
Compilation error (line 13, col 37): ; expected
Compilation error (line 13, col 37): Invalid expression term ')'

I have spent 2 hours straight trying to figure out what is wrong. Help me, please.

Here is my code online.



Solution 1:[1]

Change line 13

helper(head, out var newHead);

to

var newHead = new ListNode();
helper(head, out newHead);

Solution 2:[2]

This can also happen if you're in the wrong Visual Studio version, even if the framework version is correct. Note that double-clicking the solution (.sln) file doesn't always result in the project opening in the desired VS version. If you have multiple VS versions installed, first launch the one you want and then open the solution using the File menu.

Solution 3:[3]

You can iterate over the child views of the parent view group and just gather the texts, something like this:

val parentView: ViewGroup = findViewById(R.id.parent)
for (i in 0 until parentView.childCount) {
    val view: View = parentView.getChildAt(i)
    if (view is EditText) {
        Log.d("text", view.text.toString())
    }
}

Change the R.id.parent to the correct id of course - the id of your constraint layout.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s
Solution 2 Tawab Wakil
Solution 3 cylon