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

5

10

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 m = " + m);
    }
}

class Program
{
    static void Main(string[] args)
    {
        First f = new First();
        int x = 30;
        f.fun(out x);
    }
}

but i get some errors like "Use of unassigned out parameter 'm'" and
The out parameter 'm' must be assigned to before control leaves the current method.

So what is the meaning of these errors and why it is compulsory to assign 'm' when i'm already assigned a value to x.

Radicalism answered 28/9, 2013 at 13:41 Comment(1)
You're confusing out parameters with ref parameters. fun() does not have access to the previous value of whatever m is pointing at, and thus can't multiply it. This distinction is intentional, out parameters are meant to let you have multiple return values. It's as if you were trying to create a function that multiplies the value of the variable its result is being assigned to.Epicontinental
E
20

ref means that you are passing a reference to a variable that has been declared and initialized, before calling the method, and that the method can modify the value of that variable.

out means you are passing a reference to a variable that has been declared but not yet initialized, before calling the method, and that the method must initialize or set it's value before returning.

Endblown answered 28/9, 2013 at 13:48 Comment(2)
What if the argument being passed is not a reference, but a value type like int m as in the op? Is the parameter then implicitly "boxed" by the compiler, or perhaps implemented as an unsafe block (I suppose disassembling with ildasm would reveal)?Uveitis
My bad, by "reference" in this case, I meant the variable name, or token, that references the memory location declared by declaration of the variable. In this case I am referring to a value type, not a reference type. If it actually was a reference type, then the variable itself (or should I say the memory referenced by the variable) would not contain the data, it would contain the address of the memory where the data was stored.. In that case, "ref" means that the indirect reference, (the address) must be initialized before passing it, out means it will be initialized in the function.Endblown
A
5

You're getting an error because a variable sent to a method as an out parameter does not have to be initialized before the method call. The following is 100% correct code:

class Program
{
    static void Main(string[] args)
    {
        First f = new First();
        int x;
        f.fun(out x);
    }
}

Looks like you're looking for ref instead of out here:

class First
{
    public void fun(ref int m)
    {
        m *= 10;
        Console.WriteLine("value of m = " + m);
    }
}

class Program
{
    static void Main(string[] args)
    {
        First f = new First();
        int x = 30;
        f.fun(ref x);
    }
}
Atlantic answered 28/9, 2013 at 13:47 Comment(0)
P
2

out parameters are for when the function wants to pass a value out of itself. What you want here is ref, which is when the function expects it to be passed in, but can change it.

For examples of how both are supposed to be used, read http://www.dotnetperls.com/parameter. It's explained in simple terms, and you should be able to get a good understanding of it.

You should note that in your code, you never access the variable after the function call, therefore ref doesn't actually do anything. Its purpose is to send changes back to the original variable.

Prognostication answered 28/9, 2013 at 13:46 Comment(0)
D
2

As of C# 7.0 the ability to declare a variable right at the point where it is passed as an out argument was introduced.

Before:

public void PrintCoordinates(Point p)
{
    int x, y; // have to "predeclare"
    p.GetCoordinates(out x, out y);
    WriteLine($"({x}, {y})");
}

C# 7.0

public void PrintCoordinates(Point p)
{
    p.GetCoordinates(out int x, out int y);
    WriteLine($"({x}, {y})");
}

You can even use var key word:

p.GetCoordinates(out var x, out var y);

Be carefull with the scope of out parameter. For example, in the following code, "i" is only used within if-statement:

public void PrintStars(string s)
{
    if (int.TryParse(s, out var i)) { WriteLine(new string('*', i)); }
    else { WriteLine("Cloudy - no stars tonight!"); }
}

Further information can be found here. This link is not only about out parameter, but all the new features introduced in c# 7.0

Duplicity answered 27/8, 2016 at 15:4 Comment(0)
R
1
public void Ref_Test(ref int x)
{
    var y = x; // ok
    x = 10;
}

// x is treated as an unitialized variable
public void Out_Test(out int x)
{
    var y = x; // not ok (will not compile)
}

public void Out_Test2(out int x)
{
    x = 10;
    var y = x; // ok because x is initialized in the line above
}
Roarke answered 28/9, 2013 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.