Srinivas Rathikrindi
Srinivas Rathikrindi

Reputation: 576

How can I make a Property Read-only Declared in Interface in C#

I'm using Interfaces for Dependency Injection.

Here is my Interface

public interface IRepository 
{
    bool IsTxOpened { get; set; }
    //.....
}

And Implementation Class

public class RepositoryImpl : IRepository, IDisposable
{
    //.........
    public bool IsTxOpened { get { return _txIsOpened; } private set { _txIsOpened = value; }}
    //................
}

And Here is my Controller Class

public class EmployeeController : Controller
{
    //.........
    private IRepository _repository;

    public EmployeeController(IRepository repository)
    {           
        _repository = repository;
    }
    //...........
}

I can not mark set as private in Interface. I need Set and Get both and I do not want to expose Set to (I do not want to make it public) other classes. Is there any way to do that.

Upvotes: 1

Views: 1590

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 21795

Use this:-

public interface IRepository 
{
    bool IsTxOpened { get; }
    //.....
}

Omitting the set accessor, makes the property read-only.

Upvotes: 2

Related Questions