Presentation is loading. Please wait.

Presentation is loading. Please wait.

Properties.

Similar presentations


Presentation on theme: "Properties."— Presentation transcript:

1 Properties

2 Setting & getting the value of private attribute.
C# normally use methods known as set & get methods. These methods (knows aproperties) are made public & private ways to access or modify private data. Declaring Properties <modifier> <type> <property name> { }

3 Accessors ◆ get accessor. The get accessor is a method used to read values to a property. The get accessor does not take any parameter, but it returns a value of the data type specified in the property declaration statement. In the body of the get accessor, you need to include a return statement. ◆ set accessor. The set accessor is a method used to write values to a property. The set accessor takes a single implicit parameter named value but does not return any value..

4 public class Car { string color; public string Color1 g e t return color; } s e t color = value;

5 Car car1 = new Car(); car1.color = “Green”;
◆ read-only property. The property definition of a read-only property contains only the get accessor. ◆ write-only property. The property definition of a write-only property contains only the set accessor. ◆ read-write property. The property definition of a read-write property contains both the get and set accessors.

6 class Point { int my_X; // my_X is private int my_Y; // my_Y is private public int x get return my_X; } set my_X = value;

7 public int y { get return my_Y; } set my_Y = value;

8 class PropApp { public static void Main() Point starting = new Point(); Point ending = new Point(); starting.x = 1; starting.y = 4; ending.x = 10; ending.y = 11; Console.WriteLine("Point 1: ({0},{1})", starting.x, starting.y); Console.WriteLine("Point 2: ({0},{1})", ending.x, ending.y); }

9


Download ppt "Properties."

Similar presentations


Ads by Google