C# Error CS0547 – ‘property’ : property or indexer cannot have void type

C# Compiler Error

CS0547 – ‘property’ : property or indexer cannot have void type

Reason for the Error

You will get this error in your C# code when you have a property or indexer with a return type of void.

For example, let’s compile the below C# program

using System;

namespace DeveloperPublishConsoleCore
{
    public class Employee
    {
        public void Id   
        {
            get
            {
               // return 0;
            }
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("DeveloperPublish Hello World!");
        }
    }
}

You will receive the error code CS0547 because the class Employee contains a property Id that has a return type void.

Error CS0547 ‘Employee.Id’: property or indexer cannot have void type DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 7 Active

C# Error CS0547 – 'property' : property or indexer cannot have void type

Solution

In C#, void is not a valid return type for a property or indexer. You can fix this error in your C# program by ensuring that a valid return type is provided for the property.

using System;

namespace DeveloperPublishConsoleCore
{
    public class Employee
    {
        public int Id   
        {
            get
            {
                return 0;
            }
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("DeveloperPublish Hello World!");
        }
    }
}

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