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

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...