You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -807,6 +807,28 @@ Use the following steps to complete this section of the exercise:
807
807
808
808
The `ApplyInterest` methodcalculatestheinterestontheaccountbalanceusingthe `interestRate` fieldandaddstheinteresttothe `Balance` property. Atthispoint, the `interestRate` fieldisastaticfieldthat'ssharedamongallinstancesofthe `BankAccount` class. Sinceinterestrateisinitializedto 0, the `ApplyInterest` methoddoesn't actually apply any interest. You can update the `interestRate` field to a non-zero value in the static constructor to see the effect of the `ApplyInterest` method.
The `Balance` propertycurrentlyusesauto-implementedpropertysyntax, whichdefinesthepropertywithoutexplicitlydeclaringabackingfield. The `{ get; set; }` syntaxautomaticallycreatesaprivatebackingfieldfor the value, which is initialized to `0`. Since `Balance` is declared `public`, the value of the `Balance` property can be modified directly from outside the class. Public access to the property setter allows the balance to be updated directly without going through the `Deposit`, `Withdraw`, and `Transfer` methods. This can lead to inconsistent account balances and make it difficult to track changes to the balance.
819
+
820
+
You can convert the `Balance` property to a read-only property with a private backing field to prevent direct modification of the balance value from outside the class. This ensures that the balance can only be updated through the `Deposit`, `Withdraw`, and `Transfer` methods.
821
+
822
+
1. To convert the `Balance` property to a read-only property with a private backing field, replace the `Balance` property definition with the following code:
823
+
824
+
```csharp
825
+
826
+
public double Balance { get; privateset; } =0;
827
+
828
+
```
829
+
830
+
The `{ get; privateset; }` syntaxindicatesthatthe `Balance` propertyhasaprivatesetter, meaningthevalueofthepropertycanonlybesetfromwithinthe `BankAccount` class. The `Balance` property can still be read from outside the class, but it can only be updated through the `Deposit`, `Withdraw`, and `Transfer` methods.
831
+
810
832
1. To create a method that displays account information, add the following code:
811
833
812
834
```csharp
@@ -891,7 +913,7 @@ Use the following steps to complete this section of the exercise:
0 commit comments