Sometimes, one might want to provide a default value for the auto implemented properties. For example, you might want to provide a default text for a string value instead of null.
This can be achieved by providing the default value in the constructor for the properties.
Below is a sample code snippet demonstrating how to do it.
How to provide a default value for Auto Implemented Property in C#?
public class Article { public Article() { Category = "Dotnet"; } public string Title { get; set; } public string Category { get; set; } public DateTime ArticleDate { get; set; } }
Leave a Reply