Friday, February 16, 2018

c# constructors


 add a constructor to your class and then initialize there your property.
public class News
{

   // properties goes here

   public News()
   {
       Date=DateTime.Now;
   }

}
A far better constructor would be the following
public News(int newsId, string title, string content)
{
    NewsId=newsId;
    Title=title;
    Content=content;
    Date=DateTime.Now;
}
That way you could create an object of type News in a single line of code.
News news = new News(1,"title1","whatever");

No comments:

Post a Comment