Hello everyone. Let's imagine a situation that I have a class inside which there is a property. The property does not equate the value of a variable if the length of the incoming value is greater than the specified condition.
Filling the array
If you run this example and make the description of the book very large, the object will fall into an array, there will be no description in it. How can I forbid creating such an object?
C#:
public class Book
{
string bookDescription = null;
public string TitleOfBook { get; set; }
public string BookDescription
{
get
{
return bookDescription;
}
set
{
if (value.Length > 20)
{
Console.WriteLine("Description is too long");
}
else
{
bookDescription= value;
}
}
}
}
C#:
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Book[] books = new Book[5];
for (int i = 0; i < books.Length; i++)
{
Console.WriteLine($"Create book number {i}");
Book tempBook = new Book
{
TitleOfBook = Console.ReadLine(),
BookDescription = Console.ReadLine()
};
books[i] = tempBook;
}
for (int i = 0; i < books.Length; i++)
{
Console.WriteLine($"Title of book {books[i].TitleOfBook} \nDescription\n{books[i].TitleOfBook}");
}
}
}
If you run this example and make the description of the book very large, the object will fall into an array, there will be no description in it. How can I forbid creating such an object?