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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With