0% found this document useful (0 votes)
9 views3 pages

Property Exercise, Example

The document discusses the implementation of properties in C# using a Bicycle class example. It contrasts a traditional getter and setter approach with a property-based approach, highlighting the benefits of using properties for encapsulation and ease of access. The code demonstrates how properties simplify the management of speed and access counters in the Bicycle class.

Uploaded by

subhanpc10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Property Exercise, Example

The document discusses the implementation of properties in C# using a Bicycle class example. It contrasts a traditional getter and setter approach with a property-based approach, highlighting the benefits of using properties for encapsulation and ease of access. The code demonstrates how properties simplify the management of speed and access counters in the Bicycle class.

Uploaded by

subhanpc10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROPERTY (in C# language)

**The C# Property and implementation of Property we already discussed in Class so here is the

Important exercise we must understand

Why We Need Property See this below example

Program without using Property


using System;
02:
03: class Bicycle
04: {
05: const byte MaxSpeed = 40;
06: private byte speed = 0;
07: private int speedAccessCounter = 0;
08:
09: public byte GetSpeed ()
10: {
11: speedAccessCounter++;
12: return speed;
13: }
14:
15: public void SetSpeed(byte newSpeed)
16: {
17: if (newSpeed > MaxSpeed)
18: [Link] (“Error. {0} exceeds the speed limit {1}”,
19: newSpeed, MaxSpeed);
20: else if (newSpeed < 0)
21: [Link] (“Error. {0} is less than 0”, newSpeed);
22: else
23: speed = newSpeed;
24: }
25:
26: public int GetSpeedAccessCounter ()
27: {
28: return speedAccessCounter;
29: }
30: }
32: class BicycleTester
33: {
34: public static void Main()
35: {
36: byte speedInMilesPerHour;

Bicycle myBike = new Bicycle();


38:
39: [Link](60);
40: [Link](30);
41: [Link](“Current speed of myBike: {0}”, [Link]());
42: speedInMilesPerHour = (byte)([Link] () * 0.621);
43: [Link](“Number of times speed of myBike has been retrieved: {0}”
[Link]());
45: }
46: }

Bicycle Program Using Property


using System;
03: class Bicycle
04: {
05: const byte MaxSpeed = 40;
06: private byte speed = 0;
07: private int speedAccessCounter = 0;
09: public byte Speed
10: {
11: get
12: {
13: speedAccessCounter++;
14: return speed;
15: }
17: set
18: {
19: if (value > MaxSpeed)
20: [Link] (“Error. {0} exceeds the speed limit {1}”, value, MaxSpeed);
22: else if (value < 0)
23: [Link] (“Error. {0} is less than 0”, value);
24: else
25: speed = value;
26: }
27: }
29: public int SpeedAccessCounter
30: {
31: get
32: {
33: return speedAccessCounter; }

35: }
36: }

38: class BicycleTester


39: {
40: public static void Main ()
41: {
42: byte speedInMilesPerHour;
43: Bicycle myBike = new Bicycle ( );
45: [Link] = 60;
46: [Link] = 30;
47: [Link] (“Current speed of myBike: {0}”, [Link]);
48: speedInMilesPerHour = (byte)([Link] * 0.621);
49: [Link] (“Number of times speed of myBike has been retrieved: {0}”,)
50: [Link];
51: } }

Note: Any Query or misunderstood anything from the above code ?? whattsapp me the LINE number .(or we discuss in
future class).

You might also like