Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# constructor gives "Method must have a return type" [closed]

I been trying to learn how to create a class in C#. I created a class and than I tried to create a constructor to go along with the class. But when I created the constructor in the class, the compiler keeps thinking I'm trying to create a method instead.

public Product(string code, string description, decimal price)
{
    this.Code = code;
    this.Description = description; 
    this.Price = price;
}

Error 1 Method must have a return type

In my form, I tried to instantiate a object to go along with it.

ProductClass product1 = new Product("CS10", "Murach's C# 2010", 54.60m);

But it's still giving me an error.

Why isn't my compiler recognizing that I'm trying to create a constructor instead of a method? Is it because I don't have a accessor property to go along with it? Thank you.

like image 898
Nemox42 Avatar asked Dec 05 '22 04:12

Nemox42


1 Answers

Constructor name must be same with the class which it has defined.
If your class name is ProductClass, then change your construcor definition as:

public ProductClass(string code, string description, decimal price)
    {
        this.Code = code;
        this.Description = description; 
        this.Price = price;
    }

Have a look at this for more detail.

like image 160
Farhad Jabiyev Avatar answered May 28 '23 21:05

Farhad Jabiyev