c# - What is the purpose of accessors? -


can me understand get & set?
why needed? can make public variable.

warning: assuming know object-oriented programming.

what are properties?

properties language elements allow avoid repetitive getxyz() accessors , setxyz() mutators techniques found in other languages, java.

why exist?

they aim solve following problems:

  1. saying get , set in beginning of every access or mutation of value annoying , distracting.

    in java, say:

    class person {     private int _age;     public void setage(int value) { /*check value first, set _age*/ }     public int getage() { return this._age; } } 

    and consistently say:

    if (person.getage() > blah || person.getage() < 10) {     person.setage(5); } 

    after while, get , set become rather annoying.

  2. providing direct access actual variable breaks encapsulation, that's not option.

how used?

they used just variables. read/write them variables.

how created?

they created methods. define pair of methods that:

  1. return current value of property. oftentimes, nothing more following:

    class person {     private int _age; //declare backing field      public int age     {         { return this._age; }         set { ... }     } } 
  2. set value of property:

    class person {     public int age     {         { ... }         set         {             if (value < 0) //'value' user provided             { throw new argumentoutofrangeexception(); } //check validity             this._age = value;         }     } } 

other notes:

auto-implemented properties

c# 3.0 introduced auto-implemented properties:

public int age { get; set; } 

this equivalent to:

private int _age; //the name auto-generated public int age { { return this._age; } set { this._age = value; } } 

why exist?

it helps avoiding breaking changes in client executables.

let's you're lazy , don't want type whole thing, , decide expose variable publicly. create executable reads or writes field. change mind , decide in fact needed property, change one.

what happens?

the depending executable breaks, because code no longer valid.

auto-implemented properties avoid that, without redundancy in initial code.

indexers

indexers extend property syntax let index objects (surprise!), arrays.
c++ users: similar overloading operator [].

example:

private int[] _elements;  public int this[int index] //indexed property {     { return this._elements[index]; }     set     {         //do checks on index , value         this._elements[index] = value;     } } 

you use them obj[5] = 10;, equivalent calling set method of obj's indexer.
in fact, system.collections.generic.list<t> indexed:

var list = new list<int>(); list.add(10); list[0] = 5;  //you're indexing list, though array! 

isn't neat? :)

anything else?

there many more features properties, not of available in c#:

  • parametrized properties, of indexers special kind
  • getter/setter access modifiers (in c#)
  • multiple getters or setters (not in c#)
  • et cetera

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -