C# Error CS0177 – The out parameter ‘parameter’ must be assigned to before control leaves the current method

C# Compiler Error

CS0177 – The out parameter ‘parameter’ must be assigned to before control leaves the current method

Reason for the Error

You will receive this error when you have a function with the parameter marked with the out keyword and you have not assigned a value to that input parameter with-in the method body.

For example, try compiling the below code snippet

using System;

namespace DeveloperPubNamespace
{
    class Program
    {
        public static void GetData(out int input)   // CS0177  
        {
            Console.WriteLine("Inside GetData"); 
        }
        static void Main(string[] args)
        {

        }
    }
}

The above code snippet will result with the error code CS0177

Error CS0177 The out parameter ‘input’ must be assigned to before control leaves the current method ConsoleApp3 C:\Users\Senthil\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 7 Active

Solution

To fix the error, ensure that the input parameter that is marked with the out keyword is assigned a value in the method body of your C# code.

For example, the below code snippet fixes the error code CS0177 that was caused earlier

using System;

namespace DeveloperPubNamespace
{
    class Program
    {
        public static void GetData(out int input)   // CS0177  
        {
            input = 1;
            Console.WriteLine("Inside GetData"); 
        }
        static void Main(string[] args)
        {

        }
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...