Windows Phone Developers

Showing posts with label Get and Set Methods in C#. Show all posts
Showing posts with label Get and Set Methods in C#. Show all posts

Tuesday, March 30, 2010

Accessors and mutators in C#

Accessors and mutators do the job of retrieving and storing the data to a class member. The class member will be usually private and hence cannot be accessed directly by the object.

The following code provides a hint of the same



public enum playerType { ClassA, ClassB, ClassC, Contract }
private playerType _PlayerType;
string Name;
string Age;
string Phone;


#endregion

#region properties
public playerType PlayerType
{
//accessor
get { return _PlayerType; }

//mutator
set
{
_PlayerType = value;
}

}


Here _PlayerType is declared as private. This can be however accessed in the main program as shown below:


ClassTeam CT = new ClassTeam();
CT.PlayerType = ClassTeam.playerType.Contract; //Calls the Mutator
MessageBox.Show(CT.PlayerType.ToString()); // Calls the Accessor


The statement CT.PlayerType = ClassTeam.playerType.Contract; Calls the Mutator (set property) and sets the value and CT.PlayerType.ToString() uses Get property and retrieves the value Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Saturday, October 18, 2008

Properties in .NET / Get and Set Methods in .NET

Properties in C# / Get and Set Methods in C#


Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

class ClsTea

{

private string _locale;

public string Locale

{

get { return _locale; }

set { _locale = value; }

}

}

The following code sets the property and then gets it back

ClsTea MyTea = new ClsTea();

MyTea.Locale = "Ooty";

Console.WriteLine("British Tea is exported from " + MyTea.Locale);

  • Proper
  • ties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
  • A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels.
  • The value keyword is used to define the value being assigned by the set indexer.
  • Properties that do not implement a set method are read only.
  • For simple properties that require no custom accessor code, consider the option of using auto-implemented properties.

Properties in C# , Get and Set Methods in C# , Properties in .NET, Get and Set Methods in .NET, C# Get Method, C# Set Method

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon