user3810961
user3810961

Reputation: 61

Call only Get Accessors of property

please tell me how can i call only the get method of this property in another method . for example

 public List<EmployeeData> LOP
        {
            get
            {
                if (_lop == null)
                {
                    _lop = new List<DTPackage>();
                }

                return _lop;
            }
            set
            {
                _lop = value;
            }

        }

i want to call only get method of this property.

Upvotes: 1

Views: 71

Answers (2)

KarmaEDV
KarmaEDV

Reputation: 1691

These are all compilable variants of .Net properties:

// Shorthand
public string MyProperty1 { get; set; }
public string MyProperty2 { get; private set; }
public string MyProperty3 { get; }

// With backing field
private string _myProperty4;
private string _myProperty5;
private readonly string _myProperty6;

public string MyProperty4
{
    get { return _myProperty4; }
    set { _myProperty4 = value; }
}

public string MyProperty5
{
    get { return _myProperty5; }
    private set { _myProperty5 = value; }
}

public string MyProperty6
{
    get { return _myProperty6; }
}

MSDN

Usage:

string myString = MyProperty4; // Calls get on MyProperty4
MyProperty4 = "Hello World"    // Calls set on MyProperty4

MyProperty6 = "Hello World"    // Will not be compilable

Upvotes: 0

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

 public List<EmployeeData> LOP
 {
        get
        {
            if (_lop == null)
            {
                _lop = new List<DTPackage>();
            }

            return _lop;
        }
        set
        {
            _lop = value;
        }
 }

var lop = LOP;       // here POP get will be called
LOP = myEmployeeList //here POP set will be called

You can make set to private to avoid access from other classes or remove set for readonly

Upvotes: 2

Related Questions