-1

My question is: Is there any way that I can access constructor property after creating a new object.

I have created this class:

class Book
{
    private string Title { get; set; }
    private string Author { get; set; }
    private string ISBN { get; set; }
    public bool Available { get; set; }

    public Book(string title, string author, string isbn)
    {
        this.Title = title;
        this.Author = author;
        this.ISBN = isbn;   
    }
}

Then I create a new object in a following way:

Book myBook = new Book("Strategic Management", "John Doe", "1234567");

How can I extract title from the object above? I need to use the Title in the following syntax:

    public void RemoveLoan(Book oldLoan)
    {

        var item = Loans.SingleOrDefault(x => x.Book.Title == oldLoan.Title);
        if (item != null)
        {
            Loans.Remove(item);
        }

    }

Thanks in advance

Igor
  • 9

1 Answers1

2

Mostly, only setter method is encapsulated in properties :

// setter is encapsulated, while getter is public allowing you to acess value
public string Title { get; private set; }
public string Author { get; private set; }
public string ISBN { get; private set; }

Alternatively you can use readonly fields to make your class immutable :

public readonly title;
public readonly author;
public readonly iSBN;
Community
  • 1
  • 1
Fabjan
  • 13,506
  • 4
  • 25
  • 52