Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

out

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.

like image 566
Sachin Kumar Avatar asked Sep 28 '13 13:09

Sachin Kumar


People also ask

What does too many parameters mean?

If you receive the error "Too many parameters" at a Windows command line, verify the command you are typing is correct and there are no additional spaces in the command line. This error, as the name implies, indicates that there are too many parameters (words or commands) in the command being typed.

Why do we use out parameter?

It is generally used when a method returns multiple values. Important Points: It is similar to ref keyword. But the main difference between ref and out keyword is that ref needs that the variable must be initialized before it passed to the method.

What is out parameter in C sharp?

The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition​ as well as in the calling method.

Should we initialize an out parameter before a method returns?

Generally speaking, out parameters must be initialized before the called method returns control to the caller. However, as practice shows, the compiler can make its own adjustments to this requirement. In some cases, a low-level warning will be issued instead of a compilation error.


2 Answers

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.

like image 67
Charles Bretana Avatar answered Nov 15 '22 15:11

Charles Bretana


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);
    }
}
like image 43
MarcinJuraszek Avatar answered Nov 15 '22 13:11

MarcinJuraszek