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

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