Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

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)
        {

        }
    }
}