11,570 questions
A more succinct way is to have a property that has a public getter and private setter:
public int MyProp { get; private set; }
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I need to make certain data members in a class read only to parent classes. I'm currently doing this to protect data. It's a LOT of duplicate code / writing...
Certainly there's a better way to do than what I'm currently doing below?
// Currently using method below...
public class example1
{
private int data;
public int Data() { return data; }
}
// Tried this but only constructor can modify value and not member functions of class.
public class example2
{
public int data {get;}
public readonly int data;
}
A more succinct way is to have a property that has a public getter and private setter:
public int MyProp { get; private set; }