如何从用户输入创建对象 - C#

Dza*_*anu 3 c# user-input

我是OOP的初学者,最近开始用C#编写代码.(在Visual Studio 2017控制台应用程序中工作.)在搜索教程后,我会在这里试试运气.

我正在尝试制作一个简单的程序,允许用户通过询问输入来制作新的对象书(或多个).我知道如何手动创建类的对象,但不知道如何对用户输入执行相同的操作.这是我到目前为止的代码:

public class book
{
    //variables
    private string title;
    private string author;
    private string genre;
    private string series;

    //constructor
    public book(string _titel, string _author, string _genre, string _series)
    {
        this.titel = _titel;
        this.author = _author;
        this.genre = _genre;
        this.series = _series;
    }

    //method to ask user for input to create book
    public void createBook()
    {

    }
Run Code Online (Sandbox Code Playgroud)

Rya*_*ett 7

Console.WriteLine("Input Title: ");
var title = Console.ReadLine();
Console.WriteLine("Input Author: "):
var author = Console.ReadLine();
etc..
var book = new Book(title,author etc...)
Run Code Online (Sandbox Code Playgroud)

您将输入作为变量,然后从这些变量构造您的对象.