C Sharp/Class Properties

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Home

Basics
Home
introduction
Overview
Statements
Data Types
Variables
Operators
Flow Control
Variables II
Functions
C# - Classes and Objects I

Dates and Times
Conversions

Advanced
Classes II
Inheritance
GUI
Namespaces
Exceptions
Threads
ADO.NET
Generics

[edit] C# Properties

A program needs a way to access and manipulate private variables in a class. To do this we would rely on get and set accessors in C# properties. C# properties are methods that give the client the ability to set and get private variables in a class. A good example of this idea is accessing or adjusting a customers phone number.

To access a private variable the public get accessor must be used which is a property(method) that uses the keyword get. Its only job is to return the value of the private class variable. To set a private class variable the public set accessor must be used and it's just a property (method) that has the keyword set in it.

Public properties can access and manipulate private variables. So, it seems that private class variables are not private at all if they can be accessed by a public property. You may be thinking why do I need to use set and get accessors to gain access or change to private variables. Well, it is simple -- the set accessor can reject attempts to set a persons age to a negative number, and an attempt to set the hour of the day to 31 would be declined. The get accessor may need to retrieve a customers phone number from the database and format it so that it is human readable. You can gain access to private variables via the set and get accessors, but the accessors can control what can be done to the private class variables and how it is displayed.


In the example below you will enter your age and it will be saved and processed in a C# property. It will test whether or not the age is less than 0. If the MyAge variable is less than 0 then the result will be zero. Run the following code:

  1. using System;
  2.  
  3. class Program
  4. {
  5. public class MyClass
  6. {
  7. private int MyAge;
  8. public int Age //Age property
  9. {
  10. get //public get accessor
  11. {
  12. return MyAge;
  13. }
  14. set //public set accessor
  15. {
  16. //if MyAge is greater than or equal to
  17. //zero then 'MyAge' equals value
  18. if (value >= 0)
  19. MyAge = value;
  20. else
  21. MyAge = 0;
  22. }
  23. }
  24.  
  25. }
  26. static void Main(string[] args)
  27. {
  28. MyClass ageObject = new MyClass(); //class obect
  29. Console.Write("Enter your age: ");
  30. ageObject.Age = Convert.ToInt16(Console.ReadLine());
  31. Console.WriteLine("");
  32.  
  33. Console.WriteLine("Your age is: " + ageObject.Age);
  34. Console.Read();
  35. }
  36. }

Line 30: The Age property is called and the set accessor is executed and tests whether the values are less then zero. Then it assigns the MyAge variable to the appropriate value.

Line 33: The Age property is called and the get accessor is executed at line 10.