我是C#的新手,我已经看到了使用构造函数的不同样式。但有些教程已有一年多了。今天的最佳做法是什么?
class Book
{
//Class properties
private string title;
private int pages;
变化1:
public Book(string title, int pages)
{
this.title = title;
this.pages = pages;
}
变化2:
public Book(string _title, int _pages)
{
title = _title;
pages = _pages;
}
变化3:
public Book(string bookTitle, int numberOfPages)
{
title = bookTitle;
pages = numberOfPages;
}
}
答案 0 :(得分:3)
这是最受欢迎的方式
class Book
{
//Class properties
private string _title;
private int _pages;
public Book(string title, int pages)
{
_title = title;
_pages = pages;
}
}