我有一个带有这个构造函数的类,显然它似乎不起作用。在它出现之前,我只是忘记了语法,你能不能提醒我该怎么做,拜托?
public class Excerscise
{
Excerscise(int t, double w, DateTime d) : tries(t), weight(w), date(d) {}
int tries;
double weight;
DateTime date;
}
答案 0 :(得分:1)
类似的东西:
public class Excerscise {
int tries;
double weight;
DateTime date;
// it seems, that the constructor should be public
public Excerscise(int t, double w, DateTime d) {
tries = t;
weight = w;
date = d;
}
}
答案 1 :(得分:1)
C#不支持像这样的字段初始化程序语法(也许你来自C ++背景?)。这样做:
public class Exercise
{
int tries;
double weight;
DateTime date;
Exercise(int t, double w, DateTime d)
{
tries = t;
weight = w;
date = d;
}
}
答案 2 :(得分:0)
一个例子
class Program
{
class C2
{
int A;
int B;
public C2(int a, int b)
{
A = a;
B = b;
}
}
static void Main()
{
C2 c = new C2(1, 2);
}
}