通常,在C#中,我们用于类级别变量的命名约定与本地级别变量不同.我建议你这样写:
class StudentInfo
{
private int _studentNo;
public int StudentNo {get => _studentNo; set => _studentNo = value; }
public int Gpa { get; set;}
public StudentInfo(int cc, int ct)
{
_studentNo = cc;
Gpa = ct;
}
}
这里面有一点,让我们再看一遍:
在我的代码中,_studentNo
以上是私有field,StudentNo
是公有property.您不应该公开字段;只公开属性,因为您可以更好地控制设置值时外部代码可以做什么.属性可以包含更多的代码,例如范围判断,而如果您公开一个字段public,它可以设置为任何内容.此外,您经常会发现像JSON序列化器和数据绑定自动化这样的东西默认只序列化/绑定属性,因此您可能会绕圈子思考"为什么我的序列化器不输出所有这些明确存在于类中的数据?噢..是的..它们都是田地,不是财产."
私有成员通常为_namedLikeThis
个,并且仅在班级内被引用.在这门课上,你可以参考_studentNo
或StudentNo
;两者都可以.在课外,你只能参考StudentNo
将私有成员命名为_likeThis
意味着您可以声明本地成员likeThis
,并且您可以一目了然地知道您正在查看的变量是在类级别声明的还是在本地级别声明的.C#允许您创建与类级别同名的本地级别变量,如果您不确定需要的是类级别变量,则优先使用本地变量:
class StudentInfo
{
private int studentNo; //class level var
public StudentInfo(int cc, int ct)
{
int studentNo = 0; //local var
studentNo = 1; //this sets the local var
this.studentNo = 1; //whereas this sets the class level var
}
}
这并不像在类级别的private vars前面加上一个前导下划线那么清楚,因为很容易忘记"this",代码仍然可以编译,但有一个bug.无论如何,建议您不要将local vars作为class level vars的名称,但是使用前导下划线声明class level的倾向会更短地写出"this".
public成员是NamedLikeThis
,无论是字段(不使用public字段),属性(使用而不是字段),方法等.私有方法似乎没有一个一致的形式,但常见的趋势是使用NamesLikeThis
用于类级别的方法,namesLikeThis
用于局部函数,这是在其他方法中声明的方法
上面的Gpa
也是一个属性,是一个自动,因为编译器为你创建了后备字段(代码中没有private int _gpa
),你永远不会看到它或使用它;你只使用属性.如果你想更多地参与到添加代码,例如范围代码,你需要使用StudentNo所采用的完整属性形式,但是当你只需要简单的gat/set行为时,使用auto proeprties可以给你一个干净而简单的方法来编写代码,并将备份字段的创建留给编译器.
不要创建内部类(在另一个类的{ .. }
中写入class{ ...}
).它是可行的,但经验丰富的开发人员这样做的情况是令人难以置信的有限.保持类作为顶级实体分开,这意味着我建议使用一个完整的代码,看起来像这样(我已经添加了其他建议作为注释):
class Program //typically we keep Main() inside a class called Program
{
static void Main(string[] args)
{
StudentInfo[] students = new StudentInfo[2]; //collections/arrays have plural names in c#
students[0] = new StudentInfo(100, 4);
students[1] = new StudentInfo(101, 3);
for (int i = 0; i < students.Length; i++) //use array.Length, not hard 2
{
Console.WriteLine(students[i].StudentNo);
Console.WriteLine(students[i].Gpa);
}
}
}
class StudentInfo //this class is not declared inside Program/Student
{
public int StudentNo { get; set; } //i've switched to the auto-property form for clarity
public int Gpa { get; set;} //Acronyms in C# of 3 or more letters are typically treated as one word in Pascal case, e.g Http, not HTTP
public StudentInfo(int studentNo, int gpa) //name these fully with sensible names, as they will appear in intellisense popups and may appear as named arguments. Nonsense abbreviations make it hard to use the help VS provides and hard to read the code if using named arguments
{
StudentNo = studentNo;
Gpa = gpa;
}
}