how do I add a constructor method to a class that I created
I made a book class and I now need it to be a booktest that has a constructor that initializes the properties
using System;
class Book
{
public string Title { get; set; }
public string Author { get; set; }
public DateTime Date { get; set; }
public Book(string title, string author, DateTime date)
{
Title = title;
Author = author;
Date = date;
}
public override string ToString()
{
return String.Format("Title : {0}\nAuthor : {1}\nPublished : {2}", Title, Author, Date.Year);
}
}
class Program
{
static void Main()
{
Book b = new Book("radars adventure", "Nick james", new DateTime(1989, 1, 1));
Console.WriteLine(b);
Console.ReadKey();
}
}
}
VulpesRank #55 · 29.2K reputationPosted Dec 7, 2014, 6:28 AM EST
The output is:
coleen feador6 reputationPosted Dec 7, 2014, 6:40 AM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts