HomeCSharpC# Error CS0126 – An object of a type convertible to ‘type’ is required

C# Error CS0126 – An object of a type convertible to ‘type’ is required

C# Compiler Error

CS0126 – An object of a type convertible to ‘type’ is required

Reason for the Error

You will receive this error when the signature of a property specifies the return type and you don’t return the value of the specified type in your C# code.

For example, try compiling the below code snippet.

namespace DeveloperPublishNamespace
{
    public class Employee
    {
        public int Name
        {
            set
            {
            }
            get
            {
                return;  
            }
        }
    }

    public class DeveloperPublish
    {
        public static void Main()
        { 
        }
    }
}

We have an Employee class with the property “Name”. The property has a getter which just calls return without specifying the value or type that it needs to return. This will result in the below error.

Error CS0126 An object of a type convertible to ‘int’ is required ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 12 Active

C# Error CS0126 – An object of a type convertible to 'type' is required

Solution

When you are using a getter for a property, ensure that you return the value too.

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...