C# accessor - Accessors in c# - c# - c# tutorial - c# net



Accessor Methods - gets in c# :

  • Objects have variables.
    • Because of encapsulation, those variables are generally private
    • However, the outside world may need to use those variables
    • The class implementor may choose to add a "get" method to return the value
  • The usual name of the get method is the name of the variable prefixed with the word "get"
    • getName(), getAddress(), getPhone(), getBalance()
  •  public class BankAccount {
    	private float balance;
    
    	public float getBalance()
    	{
    		return balance;
    	}
    click below button to copy the code. By - c# tutorial - team

    Accessor Methods - sets in c# :

  • Similarly, the outside world may need to set the value of an instance variable
    • The class implementor may choose to implement a set method.
    • The responsibility of the set method is to set the appropriate variable
    • WHILST MAINTAINING data integrity of the object.
  • The usual name of the set method is the name of the variable prefixed with the word "set"
    • setName(), setAddress(), setPhone(), setBalance()
  • public class BankAccount
    {
    	private String ownerName;
    
    	public void setOwnerName(String aName)
    	{
    		ownerName = aName;
    	}
    click below button to copy the code. By - c# tutorial - team
    learn c# - c# tutorial - c# accessor implementation - c# examples -  c# programs
    learn c# - c# tutorial - c# get set property - c# examples -  c# programs
    learn c# - c# tutorial - c# get set property - c# examples -  c# programs

    Accessor Methods - Indexers in c# :

  • Enable array-like access with method-like semantics
  • great for data structure classes, collections, etc.
  • learn c# - c# tutorial - c# indexers - c# examples -  c# programs
    learn c# - c# tutorial - c# indexers - c# examples -  c# programs


    Related Searches to C# Accessor