Nem*_*x42 1 c# constructor class
我一直在努力学习如何在C#中创建一个类.我创建了一个类,而不是尝试创建一个构造函数来与类一起使用.但是当我在类中创建构造函数时,编译器一直在想我正在尝试创建一个方法.
public Product(string code, string description, decimal price)
{
this.Code = code;
this.Description = description;
this.Price = price;
}
Run Code Online (Sandbox Code Playgroud)
错误1方法必须具有返回类型
在我的表单中,我试图实例化一个对象以配合它.
ProductClass product1 = new Product("CS10", "Murach's C# 2010", 54.60m);
Run Code Online (Sandbox Code Playgroud)
但它仍然给我一个错误.
为什么我的编译器不能识别我正在尝试创建构造函数而不是方法?是因为我没有配件属性吗?谢谢.
Far*_*yev 10
Constructorname必须与它定义的类相同.
如果您的类名是ProductClass,则将您的construcor定义更改为:
public ProductClass(string code, string description, decimal price)
{
this.Code = code;
this.Description = description;
this.Price = price;
}
Run Code Online (Sandbox Code Playgroud)
看看这个更详细的信息.